Batch Processing in JDBC Example | Batch Processing | addBatch()

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about Batch processing in JDBC with example program.
Here are the most important points about Batch Processing in JDBC.
If there are multiple sql operations in a jdbc program then one by one operation will be executed on database sequentially.
If the commands are executed sequentially (one by one) then the number of trips (rounds) between an application to database will be increased.
If number of trips are increased then the performance of an application will be decreased.
In order to improve the performance by reducing the number of trips, we will use Batch Processing in JDBC.
In Batch Processing, the SQL operations will be constructed as a batch and then the batch will be send to database in a single trip.
To perform the batch processing in JDBC, the Statement interface provided two methods.
addBatch()
executeBatch()
addBatch() method is used to construct a batch. Constructing a batch means, storing the SQL commands in a buffer, maintained by Statement object.
executeBatch() :
To execute the batch, we will use the executeBacth() method. When executeBatch() called, then the commands will be transferred at a time as a batch from buffer to database.
While executing the batch in database, in the middle of command fails then all remaining all commands will be cancelled and finally BatchUpdateException will be thrown. So that it is recommended to execute the batch with Transaction Management.
Here is the complete example for Batch Processing in JDBC.
package com.onlinetutorialspoint.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Jdbc_BatchProcess_Example {

    public static void main(String[] args) throws Exception {

        Connection connection = null;

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root",
                "systemuser23!");

        Statement stmt = connection.createStatement();

        stmt.addBatch("update student set sname='Rajesh' where sid=101 ");
        stmt.addBatch("insert into student values(30,'Chandra Shekhar','Banglore','30')");
        stmt.addBatch("delete from student where sid=3001");

        int[] result = stmt.executeBatch();
        int sum = 0;

        for (int i = 0; i > result.length; i++) {
            sum = sum + result[i];
        }

        System.out.println(sum + " records are effected ");

        stmt.close();
        connection.close();

    }
}

Output :
2 records are effected
Happy Learning 🙂

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions