Batch Processing in JDBC

Instead of executing a single query, we can execute a batch (group) of queries. It makes the performance fast.

The java.sql.Statement and java.sql.PreparedStatement interfaces provide methods for batch processing.

Advantage of Batch Processing

Fast Performance

Methods of Statement interface

The required methods for batch processing are given below:

Method Description
void addBatch(String query) It adds query into batch.
int[] executeBatch() It executes the batch of queries.

Example of batch processing in jdbc

Let's see the simple example of batch processing in jdbc. It follows following steps:

  • Load the driver class
  • Create Connection
  • Create Statement
  • Add query in the batch
  • Execute Batch
  • Close Connection

If you see the table user420, two records has been added.

Example of batch processing using PreparedStatement

It will add the queries into the batch until user press n. Finally it executes the batch. Thus all the added queries will be fired.

Next TopicJDBC RowSet

Hot Tutorials

Contact US

Batch Processing