JDBC中的ResultSet保持性是什么?
ResultSet保持性决定了当使用Connection接口的commit()方法提交事务(包含所述游标/ResultSet对象)时,ResultSet对象(游标)是否应该关闭或保持打开状态。
您可以使用Connection接口的setHoldability()方法设置ResultSet保持性。
con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet接口提供两个值来指定ResultSet的保持性,即CLOSE_CURSORS_AT_COMMIT和HOLD_CURSORS_OVER_COMMIT。
让我们使用下面所示的CREATE语句在MySQL数据库中创建一个名为MyPlayers的表:
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用INSERT语句在MyPlayers表中插入7条记录:
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
CLOSE_CURSORS_AT_COMMIT
如果将ResultSet对象的保持性设置为该值。每当您使用Connection接口的commit()方法提交/保存事务时,在当前事务中创建的(已打开的)ResultSet对象将被关闭。
下面的JDBC程序将保持性值设置为CLOSE_CURSORS_AT_COMMIT,通过禁用自动提交启动事务,将名为MyPlayers的表的內容检索到ResultSet对象,将新行插入到ResultSet(以及表中),并提交事务。
提交事务后,您可以使用isClosed()方法验证在事务期间检索到的ResultSet对象是关闭的还是保持打开状态。
如果您这样做,因为我们已将ResultSet保持性设置为**CLOSE_CURSORS_AT_COMMIT**,您会发现事务保存/提交后,ResultSet对象rs已关闭。
示例
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResultSetHoldability_CloseCursorsAtCommit { 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://127.0.0.1/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); //Creating a Statement object Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //Retrieving the data ResultSet rs = stmt.executeQuery("select * from MyPlayers"); System.out.println("Contents of the table"); while(rs.next()) { System.out.print("ID: "+rs.getString("ID")+", "); System.out.print("First_Name: "+rs.getString("First_Name")+", "); System.out.print("Last_Name: "+rs.getString("Last_Name")); System.out.print("Date_Of_Birth: "+rs.getString("Date_Of_Birth")+", "); System.out.print("Place_Of_Birth: "+rs.getString("Place_Of_Birth")); System.out.print("Country: "+rs.getString("Country")); System.out.println(""); } //Inserting a new row rs.moveToInsertRow(); rs.updateInt(1, 8); rs.updateString(2, "Ishant"); rs.updateString(3, "Sharma"); rs.updateDate(4, new Date(904694400000L)); rs.updateString(5, "Delhi"); rs.updateString(6, "India"); rs.insertRow(); //Committing the transaction con.commit(); boolean bool = rs.isClosed(); if(bool) { System.out.println("ResultSet object is closed"); } else { System.out.println("ResultSet object is open"); } } }
输出
Connection established...... Contents of the table ID: 1, First_Name: Shikhar, Last_Name: DhawanDate_Of_Birth: 1981-12-05, Place_Of_Birth: DelhiCountry: India ID: 2, First_Name: Jonathan, Last_Name: TrottDate_Of_Birth: 1981-04-22, Place_Of_Birth: CapeTownCountry: SouthAfrica ID: 3, First_Name: Kumara, Last_Name: SangakkaraDate_Of_Birth: 1977-10-27, Place_Of_Birth: MataleCountry: Srilanka ID: 4, First_Name: Virat, Last_Name: KohliDate_Of_Birth: 1988-11-05, Place_Of_Birth: MumbaiCountry: India ID: 5, First_Name: Rohit, Last_Name: SharmaDate_Of_Birth: 1987-04-30, Place_Of_Birth: NagpurCountry: India ID: 6, First_Name: Ravindra, Last_Name: JadejaDate_Of_Birth: 1988-12-06, Place_Of_Birth: NagpurCountry: India ID: 7, First_Name: James, Last_Name: AndersonDate_Of_Birth: 1982-06-30, Place_Of_Birth: Burnley Country: England ResultSet object is closed
HOLD_CURSORS_OVER_COMMIT
如果将ResultSet对象的保持性设置为该值。每当您使用Connection接口的commit()方法提交/保存事务时,在当前事务中创建的(已打开的)ResultSet对象将保持打开状态。
下面的JDBC程序将保持性值设置为HOLD_CURSORS_OVER_COMMIT,通过禁用自动提交启动事务,将名为MyPlayers的表的內容检索到ResultSet对象,将新行插入到ResultSet(以及表中),并提交事务。
提交事务后,您可以使用**isClosed()**方法验证在事务期间检索到的ResultSet对象是关闭的还是保持打开状态。
如果您这样做,因为我们已将ResultSet保持性设置为**HOLD_CURSORS_OVER_COMMIT**,您会发现事务保存/完成 后,ResultSet对象rs保持打开状态。
示例
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResultSetHoldability_Hold_Cursors_Over_Commit { 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://127.0.0.1/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 HOLD_CURSORS_OVER_COMMIT con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT); //Creating a Statement object Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //Retrieving the data ResultSet rs = stmt.executeQuery("select * from MyPlayers"); System.out.println("Contents of the table"); while(rs.next()) { System.out.print("ID: "+rs.getString("ID")+", "); System.out.print("First_Name: "+rs.getString("First_Name")+", "); System.out.print("Last_Name: "+rs.getString("Last_Name")); System.out.print("Date_Of_Birth: "+rs.getString("Date_Of_Birth")+", "); System.out.print("Place_Of_Birth: "+rs.getString("Place_Of_Birth")); System.out.print("Country: "+rs.getString("Country")); System.out.println(""); } //Inserting a new row rs.moveToInsertRow(); rs.updateInt(1, 8); rs.updateString(2, "Ishant"); rs.updateString(3, "Sharma"); rs.updateDate(4, new Date(904694400000L)); rs.updateString(5, "Delhi"); rs.updateString(6, "India"); rs.insertRow(); //Committing the transaction con.commit(); boolean bool = rs.isClosed(); if(bool) { System.out.println("ResultSet object is closed"); } else { System.out.println("ResultSet object is open"); } } }
输出
Connection established...... Contents of the table ID: 1, First_Name: Shikhar, Last_Name: DhawanDate_Of_Birth: 1981-12-05, Place_Of_Birth: DelhiCountry: India ID: 2, First_Name: Jonathan, Last_Name: TrottDate_Of_Birth: 1981-04-22, Place_Of_Birth: CapeTownCountry: SouthAfrica ID: 3, First_Name: Kumara, Last_Name: SangakkaraDate_Of_Birth: 1977-10-27, Place_Of_Birth: MataleCountry: Srilanka ID: 4, First_Name: Virat, Last_Name: KohliDate_Of_Birth: 1988-11-05, Place_Of_Birth: MumbaiCountry: India ID: 5, First_Name: Rohit, Last_Name: SharmaDate_Of_Birth: 1987-04-30, Place_Of_Birth: NagpurCountry: India ID: 6, First_Name: Ravindra, Last_Name: JadejaDate_Of_Birth: 1988-12-06, Place_Of_Birth: NagpurCountry: India ID: 7, First_Name: James, Last_Name: AndersonDate_Of_Birth: 1982-06-30, Place_Of_Birth: Burnley Country: England ResultSet object is open