Java Connection getHoldability() 方法及示例
ResultSet 的可持有性决定了当使用 Connection 接口的 commit() 方法提交事务(包含该游标/ResultSet 对象)时,ResultSet 对象(游标)是否应该关闭或保持打开状态。
**Connection** 接口的 **getHoldability()** 方法用于检索并返回此连接中 ResultSet 对象的当前可持有性值。
此方法返回一个整数,表示当前 ResultSet 的可持有性,其值为 1 或 2,其中:
1 表示值 HOLD_CURSORS_OVER_COMMIT。
如果 ResultSet 对象的可持有性设置为此值,则每当您使用 Connection 接口的 commit() 方法提交/保存事务时,当前事务中创建的(已打开的)ResultSet 对象将保持打开状态。
2 表示值 CLOSE_CURSORS_AT_COMMIT。
如果 ResultSet 对象的可持有性设置为此值,则每当您使用 Connection 接口的 commit() 方法提交/保存事务时,当前事务中创建的(已打开的)ResultSet 对象将被关闭。
要检索当前的可持有性值 -
使用 DriverManager 类的 registerDriver() 方法注册驱动程序,如下所示:
//Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver());
使用 DriverManager 类的 getConnection() 方法获取连接,如下所示:
//Getting the connection String url = "jdbc:mysql:///mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");
使用 Connection 接口的 getHoldability() 方法获取当前 ResultSet 可持有性值,如下所示:
con.getHoldability();
以下 JDBC 程序建立与数据库的连接并检索当前的可持有性值。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Connection_getHoldability {
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:///mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");
System.out.println("Connection established......");
//Setting the auto commit false
con.setAutoCommit(false);
//Setting the holdability to CLOSE_CURSORS_AT_COMMIT
con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
System.out.println("HOLD_CURSORS_OVER_COMMIT: "+ResultSet.HOLD_CURSORS_OVER_COMMIT);
System.out.println("CLOSE_CURSORS_AT_COMMIT: "+ResultSet.CLOSE_CURSORS_AT_COMMIT);
System.out.println("Current ResultSet holdability value is: "+con.getHoldability());
}
}输出
Connection established...... HOLD_CURSORS_OVER_COMMIT: 1 CLOSE_CURSORS_AT_COMMIT: 2 Current ResultSet holdability value is: 2
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP