简体   繁体   中英

how to load data into postgresql from a data stream via jdbc

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。

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM