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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s