Java DatabaseMetaData supportsResultSetHoldability() method with example


ResultSet holdability determines whether the ResultSet objects (cursors) should be closed or held open when a transaction (that contains the said cursor/ ResultSet object) is committed using the commit() method of the Connection interface.

The ResultSet interface provides two values to specify the holdability of a ResultSet namely −

  • CLOSE_CURSORS_AT_COMMIT: If the holdability of the ResultSet object is set to this value. Whenever you commit/save a transaction using the commit() method of the Connection interface, the ResultSet objects created in the current transaction (that are already opened) will be closed.

  • HOLD_CURSORS_OVER_COMMIT: If the holdability of the ResultSet object is set to this value. Whenever you commit/save a transaction using the commit() method of the Connection interface, the ResultSet objects created in the current transaction (that are already opened) will be held open.

The supportsResultSetHoldability() method of the DatabaseMetaData interface is used to determine whether the underlying database supports the specified ResultSet holdability. This method accepts an integer representing a ResultSet holdability value and, returns a boolean value which is −

  • True, when the underlying database supports stored porcedures.

  • False, when the underlying database doesn't support stored porcedures.

To determine whether the underlying database supports stored porcedures −

  • 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 supportsResultSetHoldability() 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 stored porcedures else it doesn't.

Following JDBC program establishes connection with MySQL database, and determines and prints whether it supports the specified ResultSet holdability.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetadata_supportsResultSetHoldability {
   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.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
      if(bool) {
         System.out.println("Underlying database supports the specified holdability");
      } else {
         System.out.println("Underlying database does not supports the specified holdability");
      }
   }
}

Output

Connection established......
Underlying database does not supports the specified holdability

Arushi
Arushi

e

Updated on: 30-Jul-2019

24 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements