Java DatabaseMetaData supportsResultSetConcurrency() method with example


While creating a Statement object you can choose the concurrency and the type of the ResultSet object using the following variant of the createStatement() method −

Statement createStatement(int resultSetType, int resultSetConcurrency)

ResultSet Concurrency

The concurrency of the ResultSet object determines whether its contents can be updated or not.

The ResultSet interface provides two values to specify the concurrency namely −

  • CONCUR_READ_ONLY: If you set this as a value of the concurrency while creating the ResultSet object you cannot update the contents of the ResultSet you can only read/retrieve them.

  • CONCUR_UPDATABLE: If you set this as a value of the concurrency while creating the ResultSet object you can update the contents of the ResultSet.

ResultSet Type

The type of the ResultSet object determines the type of the result set based on the direction you can traverse and its sensitivity. (whether the changes done in ResultSet reflect in the database)

The ResultSet interface provides three values to specify the ResultSet type namely −

  • TYPE_FORWARD_ONLY: The ResultSet object whose cursor moves only in one direction is known as forward only ResultSet. By default, JDBC result sets are forward-only result sets.

  • TYPE_SCROLL_INSENSITIVE: ResultSet the cursor moves in forward or backward directions. This type of ResultSet is insensitive to the changes that are made in the database i.e. the modifications done in the database are not reflected in the ResultSet.

  • TYPE_SCROLL_INSENSITIVE: ResultSet the cursor moves in forward or backward directions. This type of ResultSet is sensitive to the changes that are made in the database i.e. the modifications done in the database are reflected in the ResultSet.

The supportsResultSetConcurrency() method of the DatabaseMetaData interface is used to determine whether the underlying database supports the specified concurrency in combination with the given result set type.

This method accepts two integer parameters specifying the result set type and concurrency and, returns a boolean value which is −

  • True, when the underlying database supports the specified concurrency in combination with the given result set type.
  • False, when the underlying database doesn't support the specified concurrency in combination with the given result set type.

To determine whether the underlying database supports the specified concurrency in combination with the given result set type −

  • Make sure your database is up and running.
  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.
  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.
  • Finally, invoke the supportsResultSetConcurrency() method of the ResultSetMetaData interface and save the value it returns in a boolean variable say, bool. If this value is true, underlying database supports the specified concurrency in combination with the given result set type else it doesn't.

Following JDBC program establishes connection with MySQL database, and determines and prints whether it supports the specified concurrency in combination with the given result set type.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetadata_supportsResultSetConcurrency {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Determining whether the underlying database supports the specified concurrency
      boolean bool = metaData.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_UPDATABLE);
      if(bool) {
         System.out.println("Underlying database supports the specified concurrency");
      } else {
         System.out.println("Underlying database does not supports the specified concurrency");
      }
   }
}

Output

Connection established......
Underlying database support the specified concurrency

Rishi Raj
Rishi Raj

I am a coder

Updated on: 30-Jul-2019

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements