C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Batch Processing In Java

In this article, we are going to learn about batch processing. What is it and how to implement batch processing system in java?
Submitted by Jyoti Singh, on February 21, 2018

What is batch processing?

Batch processing in java is used to execute a group of queries or a batch as executing single query again and again is time taking and reduce the performance. Thus, using batch processing multiple queries can be executed at once. This increase the performance of program.

Batch Processing can be done by using statement and prepared Statement of java as these two statement provides batch processing methods to process the batch.

We have two Batch Processing methods:

  1. void addBatch(String query) : this function adds query into batch.
  2. int[] executeBatch() : this function executes the batch.

Let’s take an example to see how batch processing is done?

Here, we are taking an example of employee table where we will insert multiple field values of multiple employees.

Step 1: First, you need to create a database table inside SQL name it "employee" and the following fields in it.

Sql design structure

Step2: Make a class named "BatchProcessing" in eclipse.

Step3: follow the given code,

package logicProgramming;//your package name

import java.io.DataInputStream;//to read the input 
import java.sql.Connection;// to connect with database mysql
import java.sql.DriverManager;//to get the connection from specified url
import java.sql.PreparedStatement;//to execute query

public class BatchProcessing {
	public static void main(String[] args) {
		try {
			//to load the class 
			Connection cn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/Employee","root", "123");
			//establishing the connection with the specified schema in database (here,Employee is schema name root & 123 are user & password)
			PreparedStatement smt=cn.prepareStatement("insert into employee values(?,?,?,?)");
			//preparing parameterized query which will be executed later
			DataInputStream kb=new DataInputStream(System.in);
			String ans=null;

			//this loop will continue until the user say no to record insert 
			do { 
				System.out.println("Enter Employee Id :");
				System.out.println("Enter Employee Name :");
				System.out.println("Enter Employee position :");
				System.out.println("Enter Employee Salary :");
				System.out.println("Do You Want to Add More Record..\n1.Yes\n2.No");
				smt.addBatch();// adding record one by one to batch }while(ans.equalsIgnoreCase("Yes")||ans.equalsIgnoreCase("1"));
				//asking user that he wants to add more record or not 
				int i[]= smt.executeBatch();
				//this will execute our batch
					System.out.println("Batches Of Record Inserted Successfully........");
					System.out.println("Batches Of Record Failed To Insert........");
		catch(Exception e)
			//this will throw error any message

Step 4: Run your code and you will see like this:

Output - java batch procession

Liked this article? Do share with your friends :)

Are you a blogger? Join our Blogging forum.