Java – MYSQL – Prepared Statements – Insert Batch

//create prepared statement, bind it to db connection and SQL query.
PreparedStatement = dbConnection.prepareStatement(insertTableSQL);
 
//For each input value set input type, input position and value.
preparedStatement.set<inputType>(<index>, <value>);
(...)
preparedStatement.set<inputType>(<index>, <value>);


//For each new row to be inserted add it to batch 
preparedStatement.addBatch();
 
//After adding to batch all rows to be inserted. 
//It's time to call execute batch.
preparedStatement.executeBatch();


For a complete example please see source.

(Source: http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/)

Advertisements