Java Batch Processing using JDBC Drivers: A Comprehensive Guide 101

By: Published: April 19, 2022

Java Batch Processing - Featured Image | Hevo Data

Java Database Connectivity (JDBC) is an API that enables Java programs to interact with databases. JDBC API is implemented through the JDBC drivers that help you access your database through Java code. Java can interact with the database using SQL or MySQL queries. Executing each SQL query after the other can cause a lot of communication overhead between the Java program and the database. Therefore, batch processing is developed to reduce such overhead and increase the speed of the Java programs. With batch processing, you can execute a group of queries in the program at once.

In this tutorial, you will learn about the steps required to set up Java Batch Processing using JDBC drivers. Read along to learn more about Java Batch Processing!

Table of Contents


  • Fundamental understanding of APIs.

What is Java Language?

Developed in 1995 by Sun Microsystems, Java is a simple, object-oriented, high-level programming language. Object-oriented programming language refers to programming languages that use classes and objects. Classes are like a prototype from which objects are created, consisting of data members and member functions. For example, consider the car as an object. The car’s name, color, brake, weight, and more will be its data members. Member functions are like functions used to define the behavior of objects of the class.

Since Java is an object-oriented programming language, it supports features like Polymorphism, Inheritance, Data Abstraction, and Data Encapsulation. Earlier, Java was invented to provide a reliable platform upon which many applications and services can be developed. Today, Java can be used to build applications for a wide range of platforms like desktops, servers, mobile phones, tablets, web browsers, and more.

Key Features of Java

1) Platform Independent

Java’s execution process consists of Java Virtual Machine (JVM), including writing, compiling, and running program execution stages. The compilation stage is done by the Java compiler called javac, included in the Java Development Kit (JDK). It takes the simple Java code as input and gives the bytecode as output. In the running stage, JVM executes this bytecode. Every operating system consists of a different JVM, but the output produced after the byte code execution is the same across all the operating systems. Therefore, Java is known as a platform-independent language.

2) Simple to Understand

Since Java is influenced by C and C++ programming, it is simple to understand. However, Java removes the complexities involved in C and C++ programming languages like explicit pointers, storage classes, operator overloading, preprocessors, header files, and more.

3) Distributed Programming

With the Java programming language, users can create distributed applications. They can split a program into many parts and store them on different computers. Programmers sitting on one machine can access all these programs. This feature in Java is called distributed programming, mainly used to develop large applications. Features like Enterprise JavaBeans (EJB) and Remote Method Innovation (RMI) help users achieve distributed programming.

4) High Performance

Java provides high performance because of its intermediate bytecode. JVM consists of a Just In Time Compiler (JIT), which compiles the bytecode to machine code at runtime. For example, if there is an XYZ instruction in a program that has to be executed more than 1000 times. In such a scenario, traditional JVM will read XYZ instruction, convert it into machine code, and execute it 1000 times. But this will be time-consuming. As a result, the JIT compiler in JVM is developed to check at a run time for the instructions executing multiple times. JIT will compile that (XYZ) instruction and keep it in the memory. Therefore, it eliminates the need for compiling the instructions again to provide high performance.

ETL Data in Minutes Using Hevo’s No-Code Data Pipeline

Hevo Data, a Fully-managed Data Integration solution, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 100+ sources straight into your Data Warehouse or any Databases. To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!


Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

What is Java Batch Processing?

Batch Processing refers to the processing or executing SQL statements grouped into a batch at once. Generally, when you execute SQL statements one after the other, the execution simultaneously switches from the database to the program. As a result, the communication overhead increases between the database and the program. Therefore, to reduce the communication overhead, batch processing is used.

For example, consider the table Emp with the below description.

Java Batch Processing - Example | Hevo Data
Image Source

You can insert data to the EMP table using SQL statements as follows.

Stmt.execute("INSERT INTO Emp VALUES ('Amit', '30-9-1989', 'Hyderabad')");
Stmt.execute("INSERT INTO Emp VALUES ('Amit', '1-9-1989', 'Vishakhapatnam')");

You can insert the same record using the PreparedStatement as follows.

String query = "INSERT INTO Emp(Name, String, Location) VALUES (?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Amit");
pstmt.setDate(2, "30-9-1989"));
pstmt.setString(3, "Hyderabad");
pstmt.setString(1, "Sumith");
pstmt.setDate(2, "1-9-1989"); //Friday, Sept 1, 1989 12:00:00 AM
pstmt.setString(3, "Vishakhapatnam");

In both the above cases, each insert statement is executed individually. When the execute() method gets invoked, the insert statement is executed in the database and reverts to the program.

This method of inserting values into tables is best for inserting fewer number values. In case you have to insert many values into the tables, you will need batch processing to add the statements to the batch using the addBatch() method and executeBatch() method of the Statement interface, as shown below.

Stmt.addBatch("INSERT INTO Emp VALUES ('Amit', '30-9-1989', 'Hyderabad')");
Stmt.addBatch("INSERT INTO Emp VALUES ('Amit', '1-9-1989', 'Vishakhapatnam')");
pstmt.setString(1, "Amit");
pstmt.setDate(2, "30-9-1989"));
pstmt.setString(3, "Hyderabad");
pstmt.setString(1, "Sumith");
pstmt.setDate(2, "1-9-1989"); //Friday, Sept 1, 1989 12:00:00 AM
pstmt.setString(3, "Vishakhapatnam");

What Makes Hevo’s Real-time Data Integration Process Unique?

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-Day Free Trial!

How to Set up Java Batch Processing?

Java Batch Processing consists of the below steps:

  1. Load the driver class.
  2. Create the connection.
  3. Create the Statement.
  4. Add a query in the batch.
  5. Execute the batch.
  6. Close the connection.

The below example consists of Java Batch Processing using JDBC.

import java.sql.*;  
class FetchRecords{  
public static void main(String args[])throws Exception{  
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
Statement stmt=con.createStatement();  
stmt.addBatch("insert into user420 values(190,'abhi',40000)");  
stmt.addBatch("insert into user420 values(191,'umesh',50000)");   
stmt.executeBatch();//executing the batch 
con.close();  }} 

You can use the DatabaseMetaData.supportsBatchUpdates() method to determine if the target database supports batch update processing. This method is returned true if your JDBC driver supports batch processing.
Whenever you want to connect your database with Java, you need interfaces to interact with your databases. Java consists of three such interfaces Statement, PreparedStatement, and CallableStatement, that define the methods and properties, enabling you to send SQL or PL/SQL commands to your database and receive data from your database.

The Statement, PreparedStatement, and CallableStatement interfaces consist of the addBatch(), executeBatch(), and clearBatch() methods. While the addBatch() method is used to add individual statements to the batch, the executeBatch() method is used for starting the execution of all the grouped statements. The executeBatch()method returns an array of integers, where the elements in the array represent the update count for the respective update statement, the clearBatch() method is used to remove statements that you have added with the addBatch() method.

A) Java Batch Processing with the Statement object

Follow the below steps to perform batching with the Statement object.

  1. Create a Statement object using createStatement() method.
  2. Set the setAutoCommit() method to false.
  3. Use addBatch() method to add as many SQL statements as you want into the batch.
  4. Use executeBatch() method to execute all the SQL statements.
  5. Use the commit() method to commit all the changes.

The below code consists of a batch processing example using the Statement object.

Statement stmt = conn.createStatement();


String SQL = "INSERT INTO Employees (id, first, last, age) " +
             "VALUES(200,'Zia', 'Ali', 30)";

String SQL = "INSERT INTO Employees (id, first, last, age) " +
             "VALUES(201,'Raj', 'Kumar', 35)";


String SQL = "UPDATE Employees SET age = 35 " +
             "WHERE id = 100";


int[] count = stmt.executeBatch();


B) Java Batch Processing with the PreparedStatement object

Follow the below steps to perform batch processing with the PreparedStatement object.

  1. Create SQL statements using placeholders. Placeholders are used as text characters in SQL statements.
  2. Create a PreparedStatement object using PreparedStatement() method.
  3. Set the setAutoCommit() method to false.
  4. Use addBatch() method to add as many SQL statements as you want into the batch.
  5. Use executeBatch() method to execute all the SQL statements.

The below code consists of a batch processing example using the PreparedStatement object.

String SQL = "INSERT INTO Employees (id, first, last, age) " +
             "VALUES(?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(SQL);
//Set auto-commit to false
// Set the variables
pstmt.setInt( 1, 400 );
pstmt.setString( 2, "Pappu" );
pstmt.setString( 3, "Singh" );
pstmt.setInt( 4, 33 );
// Add it to the batch
// Set the variables
pstmt.setInt( 1, 401 );
pstmt.setString( 2, "Pawan" );
pstmt.setString( 3, "Singh" );
pstmt.setInt( 4, 31 );
// Add it to the batch
//add more batches
//Create an int[] to hold returned values
int[] count = stmt.executeBatch();

//Explicitly commit statements to apply changes

C) Java Batch Processing with the CallableStatement object

Follow the below steps to perform batch processing with the CallableStatement object.

  1. Use the registerDriver() method of the DriverManager class to register the driver class. Pass the driver class name to the registerDriver() method as a parameter.
  2. Use the getConnection() method of the DriverManager class to connect with the database. Pass URL, username, and password to it as parameters.
  3. Use the setAutoCommit() method of the Connection interface to set the auto-commit to false. 
  4. Use the prepareCall() method of the Connection interface to create a CallableStatement object. Pass the query to it as a parameter with placeholders.
  5. Use the setter methods of the CallableStatement interface to set the values to the placeholders.
  6. Use the addBatch() method of the Statement interface to add the required statements to the batch.
  7. Use the executeBatch() method of the Statement interface to execute the batch. 
  8. Use the commit() method of the Statement interface to commit the changes.

For example, you have created the table named ‘Dispatches’ with the below descriptions.

Dispatches table
Image Source

Create a procedure called myProcedure, which stores values in the above-created table using the below command.

Create procedure myProcedure (
   IN Product_Name VARCHAR(255),
   IN Name_Of_Customer VARCHAR(255),
   IN Month_Of_Dispatch VARCHAR(255),
   IN Price INT, IN Location VARCHAR(255))
   insert into Dispatches values ();

The below program calls a procedure called myProcedure, which inserts data into the Dispatch table. It sets values to the callable statement using batch updates.

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public class BatchProcessing_CallableStatement {
   public static void main(String args[])throws Exception {
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //CREATE TABLE Dispatches( Product_Name VARCHAR(255), Name_Of_Customer
      VARCHAR(255), Month_Of_Dispatch VARCHAR(255), Price INT, Location VARCHAR(255));
      //Setting auto-commit false
      //Creating a PreparedStatement object
      CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?, ?, ?)}");
      cstmt.setString(1, "Keyboard");
      cstmt.setString(2, "Amith");
      cstmt.setString(3, "January");
      cstmt.setInt(4, 1000);
      cstmt.setString(5, "Hyderabad");
      cstmt.setString(1, "Earphones");
      cstmt.setString(2, "Sumith");
      cstmt.setString(3, "March");
      cstmt.setInt(4, 500);
      cstmt.setString(1, "Mouse");
      cstmt.setString(2, "Sudha");
      cstmt.setString(3, "September");
      cstmt.setInt(4, 200);
      cstmt.setString(5, "Vijayawada");
      //Executing the batch
      //Saving the changes
      System.out.println("Records inserted......");


Connection established......
Records inserted......

Verify the contents of the Dispatch table.

Contents of Dispatches table
Image Source

This is how you can perform Java Batch Processing in a seamless manner!


In this tutorial, you learned to set up Java Batch Processing using JDBC drivers. This tutorial focuses on performing Java Batch Processing using Statement, PreparedStatement, and CallableStatement interfaces.Batch processing is essential in financial institutions that generate large amounts of data and need to transfer it in less time. It is used in ATM transactions, credit card transactions, customer services, and more. In case you want to export data from a source of your choice into your desired Database/destination then Hevo Data is the right choice for you! 

Visit our Website to Explore Hevo

Hevo Data, a No-code Data Pipeline provides you with a consistent and reliable solution to manage data transfer between a variety of sources and a wide variety of Desired Destinations, with a few clicks. Hevo Data with its strong integration with 100+ sources (including 40+ free sources) allows you to not only export data from your desired data sources & load it to the destination of your choice, but also transform & enrich your data to make it analysis-ready so that you can focus on your key business needs and perform insightful analysis.

Want to take Hevo for a spin?

 Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

Share your experience of learning about Java Batch Processing. Let us know in the comments section below!

Manjiri Gaikwad
Freelance Technical Content Writer, Hevo Data

Manjiri loves data science and produces insightful content on AI, ML, and data science. She applies her flair for writing for simplifying the complexities of data integration and analysis for solving problems faced by data professionals businesses in the data industry.

No-code Data Pipeline For your Data Warehouse