SQL Batch Insert


Batch inserts are used to achieve high performance inserts. You may achieve up to ten times the throughput over a standard insert. A batch size of 100 may be effective.

Syntax

jdbc callpurpose
PreparedStatement.addBatch()Called after setting all the parameters for the insert statement. Basically called in place of executeUpdate()
PreparedStatement.executeBatch()Called after adding all requests to the batch. Processes the batch.

Example


PreparedStatement stmt = conn.prepareStatement("insert into persons (name, age, occupation) values ('bob', ?, ?)")
for (int i = 0; i < 100; i++) {
	stmt.setInt(1, i);
	stmt.setString(2, "artist");
	stmt.addBatch();
}
stmt.executeBatch()