JDBC Batch SQL Update Statement Example With Return Number of Effected Rows


 

JDBC Batch SQL Update Statement Example With Return Number of Effected Rows

In this example, we are discuss about update statement with return number of effected rows.

In this example, we are discuss about update statement with return number of effected rows.

JDBC Batch SQL Update Statement Example With Return Number of Effected Rows:

In this example, we are discuss about update statement with return number of effected rows.

The executebatch() method are use to execute the batch on the statement and returns an array of integers, and each element of the array represents the update count for the respective update statement.

First we will create a java class name BatchUpdateWithReturnUpdateRowNumber.java and import java.sql .*; package and create database connection between class and datasource as:

Connection connection = null;

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection(
           
"jdbc:mysql://localhost:3306/roseindia_jdbc_tutorials",
            "root"
,"root");

Now we will create Statement object and setAutoCommit(false) as:

Statement stmt = connection.createStatement();

connection.setAutoCommit(false);

Now we will create SQL statements and execute on the created statement object and store return in an integer array as:

String updatequery1 = "UPDATE user SET user_name = 'Ravi' WHERE user_id = 3";

stmt.addBatch(updatequery1);

String updatequery2 = "UPDATE user SET user_name = 'Brijesh' WHERE user_id = 2";

stmt.addBatch(updatequery2);

int count[] = stmt.executeBatch();

Now print the count array elements that represents the update count for the respective update statement.

The code of the BatchUpdateWithReturnUpdateRowNumber.java class is:

import java.sql.*;
public class BatchUpdateWithReturnUpdateRowNumber {
  public static void main(String[] args) {
    try{
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/roseindia_jdbc_tutorials",
        "root","root");
    try{            
      Statement stmt = connection.createStatement();            
      connection.setAutoCommit(false);
      String updatequery1 = "UPDATE user SET user_name = 'Ravi' WHERE user_id = 3";              
      stmt.addBatch(updatequery1);
      String updatequery2 = "UPDATE user SET user_name = 'Brijesh' WHERE user_id = 2";              
      stmt.addBatch(updatequery2);
      int count[] = stmt.executeBatch();
      System.out.println("Updated number of rows = " + count.length);
        connection.commit()
    }
    catch (SQLException s){
        System.out.println("SQL Exception " + s);
    }
    }
    catch (Exception e){
          e.printStackTrace();
    }
  }
}

Now we will run this program with the help of eclipse IDE and see the output.

Program output:

The eclipse  console output is:

The database table output before updating is:

The database table output after updating is:

Download source code

Ads