Java JDBC: Batch update using JDBC Statement Interface

In this article, we will use JDBC API to add couple of DML statements to batch and finally executing/updating the batch of statements in MySQL database from Java application (i.e.; using Statement Interface)

 

Advantage of Batch update:

  • Performance improves
  • as number of database hits becomes lesser
  • comparing with each SQL query execution

 

1. Pre-requisite:

  • Java JDK 1.8.0_77
  • MySQL database 5.5.16
  • Eclipse Luna IDE 4.4.0
  • mysql-connector-java-5.1.38.jar file

 

2. Database parameters:

Let us move on and code an example to connect MySQL database from Java application to execute/update batch of statements using JDBC API. But before that, we will list down required things to connect database

  • database server IP or address (localhost)
  • sever port (3306)
  • database name (PLAYER_INFO)
  • username (root)
  • password (root@123)

Note: All bold are database values to connect MySQL database

 

3. Batch update using JDBC Statement Interface

  • As we are ready with required things to connect MySQL database from Java application
  • We will use below methods from Statement Interface to add DML statements to batch and finally executing/updating batch of statements (batch processing or batch execution)
    1. addBatch(String sqlQuery);
    2. executeBatch();
  • Also, we will enclose all DML statement execution within JDBC transaction to maintain consistency and integrity with database
  • Let us code a simple example using Statement Interface

BatchUpdateUsingJDBCStatement.java

package in.bench.resources.mysql.db.example;

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

public class BatchUpdateUsingJDBCStatement {

	public static void main(String[] args) {

		// variables
		Connection connection = null;
		Statement statement = null;

		// Step 1: Loading or registering MySQL JDBC driver class
		try {
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch(ClassNotFoundException cnfex) {
			System.out.println("Problem in loading MySQL JDBC driver");
			cnfex.printStackTrace();
		}

		// Step 2: Opening database connection
		try {

			// Step 2.A: Create and get connection using DriverManager
			connection = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/PLAYER_INFO", "root", "root@123"); 

			// Start of JDBC transaction
			connection.setAutoCommit(false);

			// Step 2.B: Creating JDBC Statement 
			statement = connection.createStatement();

			// create SQL query to update PLAYER info
			String sqlUpdateQuery_1 = 
                    "UPDATE PLAYER SET AGE = 46 WHERE PLAYER_ID = 2";

			// add to batch for execution
			statement.addBatch(sqlUpdateQuery_1);

			// again create SQL query to update another PLAYER info
			String sqlUpdateQuery_2 = 
                       "UPDATE PLAYER SET AGE = 35 WHERE PLAYER_ID = 3";

			// add to batch for execution
			statement.addBatch(sqlUpdateQuery_2);

			// again create SQL query to update another PLAYER info
			String sqlUpdateQuery_3 = 
   "UPDATE PLAYER SET NAME = 'Graeme Craig Smith' WHERE PLAYER_ID = 6";

			// add to batch for execution
			statement.addBatch(sqlUpdateQuery_3);

			// Step 2.C: Executing SQL and retrieve data into ResultSet
			statement.executeBatch();

			// End of JDBC transaction
			connection.commit();

			System.out.println("
                  All PLAYER info updation is successful");
		}
		catch(SQLException sqlex){
			sqlex.printStackTrace();
		}
		finally {

			// Step 3: Closing database connection
			try {
				if(null != connection) {

					// cleanup resources, once after processing
					statement.close();

					// and then finally close connection
					connection.close();
				}
			}
			catch (SQLException sqlex) {
				sqlex.printStackTrace();
			}
		}
	}
}

Output:

All PLAYER info updation is successful

 

4. Download:

 

Read Also:

 

References:

 

Happy Coding !!
Happy Learning !!

Java JDBC: Batch insert using JDBC Statement Interface
Java JDBC: Dropping a table using JDBC Statement Interface