how to load data into postgresql from a data stream via jdbc we will get a data stream or a Array in memory, Is there any method to load the stream data into postgresql? use insert is too much inefficient。
Asked
Active
Viewed 1,873 times
1 Answers
2
You'll want to use a prepared statement with a batch insert. Have a look at the page: http://viralpatel.net/blogs/batch-insert-in-java-jdbc/, which describes both the performance and security benefits of this approach. The code below came from that page.
String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
ps.setString(1, employee.getName());
ps.setString(2, employee.getCity());
ps.setString(3, employee.getPhone());
ps.addBatch();
if(++count % batchSize == 0) {
ps.executeBatch();
}
}
ps.executeBatch(); // insert remaining records
ps.close();
connection.close();

phatfingers
- 9,770
- 3
- 30
- 44
-
thanks for your reply,but I want to know if there's some way I can use the method like Statement stmt=conn.createStatement() stmt.setLocalInfileInputStream(InputStream input) in Mysql jdbc – moxpeter Jun 26 '12 at 05:09
-
`Statement stmt=conn.createStatement() stmt.setLocalInfileInputStream(InputStream input) ` – moxpeter Jun 26 '12 at 05:15