JDBC Batch clearBatch Method Example


 

JDBC Batch clearBatch Method Example

The clearBatch() used for remove the all statements from the batch that are added by using addBatch() in the Batch.

The clearBatch() used for remove the all statements from the batch that are added by using addBatch() in the Batch.

JDBC Batch clearBatch Method Example:

The clearBatch() used for remove the all statements from the batch that are added by using addBatch() in the Batch but we can not remove selective choose statements from the batch. In this example we are discuss about it.

Syntax:

stmt.cleareBatch();

The stmt is the object of the Statement. This method have void return type.

In this example we are create three insert statements and after two insert statements we are call stmt.clearBatch() that remove all the statements form the batch and after that we are add third insert statement as:

              String insertquery1 = 

"INSERT INTO user (user_id, user_name) VALUES(1,'Brijesh')";
  stmt.addBatch(insertquery1);
             
  String insertquery2 = "INSERT INTO user (user_id, user_name)
VALUES(2,'Raj')"
;
  stmt.addBatch(insertquery2);
              
  stmt.clearBatch();
             
  String insertquery3 = "INSERT INTO user (user_id, user_name)
VALUES(3,'Ankit')"
;
  stmt.addBatch(insertquery3);

  After run this example you can see only third insert statement are execute. The example code is:

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

public class JDBCBatchClearMethodExample {

  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{        
          Class.forName("com.mysql.jdbc.Driver");    
          conn = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/roseindia_jdbc_tutorials","root","root");
          try{
             stmt = conn.createStatement();
             
             conn.setAutoCommit(false);
             
              String insertquery1 = "INSERT INTO user (user_id, user_name) VALUES(1,'Brijesh')";
              stmt.addBatch(insertquery1);
             
              String insertquery2 = "INSERT INTO user (user_id, user_name) VALUES(2,'Raj')";
              stmt.addBatch(insertquery2);
              
              stmt.clearBatch();
             
              String insertquery3 = "INSERT INTO user (user_id, user_name) VALUES(3,'Ankit')";
              stmt.addBatch(insertquery3);
              int[] NumberOfEffectedRows = stmt.executeBatch();
              System.out.println("Number Of Effected Rows :" + NumberOfEffectedRows.length);
             
              conn.commit()
            }
            catch (SQLException s){
            System.out.println("SQL Exception " + s);
            }
          }
          catch (Exception e){
            e.printStackTrace();
          }  
     }
}

Now we will run this example and see the output.

Program output:

The eclipse console output is:

The database output is:

Program source code

Ads