0

如何通过jdbc将数据流中的数据加载到postgresql中,我们将在内存中获得一个数据流或一个数组,有没有什么方法可以将流数据加载到postgresql中?使用插入太低效了。</p>

4

1 回答 1

2

您需要使用带有批量插入的准备好的语句。看一下页面: http: //viralpatel.net/blogs/batch-insert-in-java-jdbc/,它描述了这种方法的性能和安全优势。下面的代码来自该页面。

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();
于 2012-06-26T03:54:26.307 回答