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:
- void addBatch(String query) : this function adds query into batch.
- 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.
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 {
Class.forName("com.mysql.jdbc.Driver");
//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 :");
smt.setString(1,kb.readLine());
System.out.println("Enter Employee Name :");
smt.setString(2,kb.readLine());
System.out.println("Enter Employee position :");
smt.setString(3,kb.readLine());
System.out.println("Enter Employee Salary :");
smt.setInt(4,Integer.parseInt(kb.readLine()));
System.out.println("Do You Want to Add More Record..\n1.Yes\n2.No");
ans=kb.readLine();
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
if(i!=null)
{
System.out.println("Batches Of Record Inserted Successfully........");
}
else
{
System.out.println("Batches Of Record Failed To Insert........");
}
}
}
catch(Exception e)
{
//this will throw error any message
System.out.println(e.getMessage());}
}
}
}
Step 4: Run your code and you will see like this: