JDBC Batch update example

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Filament.io 0 Flares ×

Need for Batch processing

Batch updates are used for INSERT, UPDATE and DELETE operations if you are going to update many records. For small number of database operations there may not be much benefit with batching. But there are serious performance benefits, if you are updating large number of records. In such cases, batching can drastically improve the performance and efficiency of the database operations. Since you send the changes in a batch, the round trip between the application and database will be significantly low and less protocol level handshaking is required.  Typically databases are located in another machine and this involves network communication and batching greatly improves the network communication overheads.

Steps Involved

  • SetAutoCommit to False
  • Invoke statement.addBatch passing it the SQL
  • Invoke statement.executeBatch
  • Commit and Close the connection

Java Source Code