Tutorialspoint

1 Answer
Arushi

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 getResultSetHoldability() method of the DatabaseMetaData interface retrieves the default holdability for the ResultSet objects of the underlying database.

This method returns an integer value representing the default ResultSet holdability, which will be either 1 or 2 where,

  • 1 indicates the value 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.
  • 2 indicates the value 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.

To get the DatabaseMetaData object

  • 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, get the underlying database's default value for the holdability of ResultSets, by invoking the getResultSetHoldability() method of the DatabaseMetaData interface.

Following JDBC program establishes connection with MySQL database, retrieves the default ResultSet holdability of the underlying database.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetadata_getResultSetHoldability {
   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();
      //Retrieving the ResultSet holdability of the current database
      int holdability = metaData.getResultSetHoldability();
      System.out.print("ResultSet holdability of the underlying database: ");
      switch(holdability) {
         case 1 :
            System.out.print("HOLD_CURSORS_OVER_COMMIT");
         break;
         case 2 :
            System.out.print("CLOSE_CURSORS_AT_COMMIT");
         break;
      }
   }
}

Output

Connection established......
ResultSet holdability of the underlying database: HOLD_CURSORS_OVER_COMMIT

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.