0

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。

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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
Statement stmt=conn.createStatement() stmt.setLocalInfileInputStream(InputStream input)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.