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,
To get the DatabaseMetaData object
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.
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; } } }
Connection established...... ResultSet holdability of the underlying database: HOLD_CURSORS_OVER_COMMIT