sql

JDBC Batch Processing Example

Photo of Chandan SinghChandan SinghJanuary 2nd, 2015Last Updated: March 5th, 2019
0 280 2 minutes read

In this example, we will see how we can use Batch Processing inJava Database Connectivity(i.e.JDBC).

When multiple inserts are to be made to the table in a database, the trivial way is to execute a query per record.
However, this involves acquiring and releasing connection every time a record is inserted, which hampers application performance.

We overcome, this(acquiring and releasingconnection every-time) by making use of Batch operations in JDBC.
 
 
 

 
We set the parameters in thejava.sql.PreparedStatement and it to the batch usingPreparedStatement.addBatch() method. Then when the batch size reaches a desired threshold, we execute the batch usingPreparedStatement.executeBatch() method.

TIP:
Another way to avoid manually releasing/acquiring connection is to use Connection Pooling.

However, when executing Queries in Batch, it is sometimes important to maintain theatomicity of the database. This can be a problem if one the commands in the batch throws some error since the commands after the exception will not be executed leaving the database in an inconsistent state. So we set the auto-commit to false and if all the records are executed successfully, we commit the transaction. This maintains the integrity of the the database.

We will try to understand points explained above with the help of an example :

BatchExample.java:

package com.javacodegeeks.examples;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.Arrays;public class BatchExample{public static void main(String[] args){try (Connection connection = DBConnection.getConnection()){connection.setAutoCommit(false);try (PreparedStatement pstmt = connection.prepareStatement("Insert into txn_tbl (txn_id,txn_amount, card_number, terminal_id) values (null,?,?,?)");){pstmt.setString(1, "123.45");pstmt.setLong(2, 2345678912365L);pstmt.setLong(3, 1234567L);pstmt.addBatch();pstmt.setString(1, "456.00");pstmt.setLong(2, 567512198454L);pstmt.setLong(3, 1245455L);pstmt.addBatch();pstmt.setString(1, "7859.02");pstmt.setLong(2, 659856423145L);pstmt.setLong(3, 5464845L);pstmt.addBatch();int[] arr = pstmt.executeBatch();System.out.println(Arrays.toString(arr));connection.commit();}catch (SQLException e){e.printStackTrace();connection.rollback();}}catch (Exception e){e.printStackTrace();}}}

DBConnection.java:

package com.javacodegeeks.examples;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/*** @author Chandan Singh*/public class DBConnection{public static Connection getConnection() throws SQLException, ClassNotFoundException{Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jcg?rewriteBatchedStatements=true", "root", "toor");return connection;}}

One important point to note here is theconnection URL. TherewriteBatchedStatements=true is important since it nudges the JDBC to pack as many queries as possible into a single network data packet, thus lowering the traffic. Without that parameter, there will not be much performance improvement when using JDBC Batch.

Want to be a JDBC Master ?
Subscribe to our newsletter and download the JDBCUltimateGuideright now!
In order to help you master database programming with JDBC, we have compiled a kick-ass guide with all the major JDBC features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

NOTE:
We can use JDBC Batch, when usingjava.sql.Statement, in similar fashion.

ThePreparedStatement.executeBatch() method retruns anint array. Depending upon the values, we can know the status of the each executed queries:

  • A value greater than zero usually indicates successful execution of query.
  • A value equal toStatement.SUCCESS_NO_INFO indicates, successful command execution but no record count is available.
  • A value equal toStatement.EXECUTE_FAILEDindicates, command was not executed successfully. It shows up only if the driver continued to execute the commands after the failed command.

Summary:

Here we tried to understand what is JDBC Batch and how we can the use the same to reduce network traffic and improve our application performance.

Download
You can download the source code of this example here:BatchOperation.zip
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Chandan SinghChandan SinghJanuary 2nd, 2015Last Updated: March 5th, 2019
0 280 2 minutes read
Photo of Chandan Singh

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.