How do I find out whether the underlying database supports batch processing?

JDBCJava 8ProgrammingObject Oriented Programming

Not all the databases support batch processing therefore before proceeding with batch updates in your application. You need to verify whether the database you are trying to communicate supports batch processing/batch updates or not.

You can do so using the supportsBatchUpdates() method of the DatabaseMetaData interface.

Follow the steps given below:

  • Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as a parameter.

  • Connect to the database using the getConnection() method of the DriverManager class. Passing URL (String), username (String), password (String) as parameters to it.

  • Create a DatabaseMetaData object using the getMetaData() method of the Connection interface.

  • Using the obtained object invoke the supportsBatchUpdates() method. This returns true if the database you have connected to supports batch updates and it returns false if it doesn’t.

Example

Following program verifies weather underlying database supports batch updates or not.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class DBSupportsBatchUpdates {
   public static void main(String args[])throws Exception {
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the DatabaseMetaData object
      DatabaseMetaData dbMetadata = con.getMetaData();
      boolean bool = dbMetadata.supportsBatchUpdates();
      if(bool) {
         System.out.println("Underlying database supports batch updates");
      } else {
         System.out.println("Underlying database doesn’t supports batch updates");
      }
   }
}

Output

Connection established......
Underlying database supports batch updates
raja
Published on 25-Mar-2019 06:41:25
Advertisements