JDBC Batch commit method Example


 

JDBC Batch commit method Example

In this example, you can learn what is setAutoCommit() method and commit() method in the jdbc batch processing and how it work?.

In this example, you can learn what is setAutoCommit() method and commit() method in the jdbc batch processing and how it work?.

JDBC Batch commit method Example:

In this example, you can learn what is setAutoCommit() method and commit() method in the jdbc batch processing and how it work?.

The setAutoCommit() method used to set created connection auto commit disable or enable by passing boolean(false or true). If we set auto-commit mode is disable then anyone SQL statement will not be committed until we do not call commit method explicitly.

Syntax:

connection.setAutoCommit(boolean);

The commit() method allow connection to commit all SQL statements.

Syntax:

connection.commit();

Using these methods we are creating code snippet that describe the working

process of these two methods.

1. First we will create a java class Batchcommit.java that import sql

package for database connection and data insertion or manipulation.

2. Now we will create data base connection as:

Connection connection = null;

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

connection = DriverManager.getConnection

("jdbc:mysql://localhost:3306/databasename","username","passsword");

3. Now we will create Statement object as:

Statement stmt = connection.createStatement();

4. Now we will set connection auto commit is disable as:

connection.setAutoCommit(false);

5. Now we will create SQL insert statement and add in the batch as:

String insertquery1 =

"INSERT INTO user (user_id, user_name) VALUES(3,'Ankit')";

stmt.addBatch(insertquery1);

6. Now we will call the executeBatch() method on the created statement object as:

stmt.executeBatch();

7. And Last we will  commit the connection as:

connection.commit();

The code of the Batchcommit.java class is:

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

public class Batchcommit {  
  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_tutorial","root","root");
    try{
      // Create statement object  
        Statement stmt = connection.createStatement();            
        // Set auto-commit to false
      connection.setAutoCommit(false);
      // insert query 
      String insertquery1 = "INSERT INTO user (user_id, user_name) VALUES(3,'Ankit')";              
      stmt.addBatch(insertquery1);
          stmt.executeBatch();
          // connection commited
          connection.commit();
          System.out.println("Batch commit example." );
       }
       catch (SQLException s){
          System.out.println("SQL Exception " + s);
      }
    }
    catch (Exception e){
        e.printStackTrace();
    }
  }
}

Program output:

The eclipse console output is:

The database table output is:

Download code

Ads