如何在JDBC的可滚动结果集中移动光标?
在JDBC中,有两个可滚动的ResultSet,即,可滚动敏感的和不可滚动敏感的。
在TYPE_SCROLL_INSENSITIVE ResultSet中,光标向前或向后移动。这种类型的ResultSet对数据库中所做的更改不敏感,即,数据库中所做的修改不会反映在ResultSet中。
这意味着如果我们已使用JDBC程序与数据库建立了连接,并检索了一个保存有名为SampleTable的表中所有记录的ResultSet。同时,如果我们在表中添加了更多记录(检索到ResultSet之后),则这些最近的更改将不会反映在我们先前获得的ResultSet对象中。
在TYPE_SCROLL_INSENSITIVE ResultSet中,光标向前或向后移动。这种类型的ResultSet对数据库中所做的更改敏感,即,数据库中所做的修改将反映在ResultSet中。
这意味着如果我们已使用JDBC程序与数据库建立了连接,并检索了一个保存有名为SampleTable的表中所有记录的ResultSet。同时,如果我们在表中添加了更多记录(在检索ResultSet之后),这些最近的更改将反映在我们先前获得的ResultSet对象中。
滚动敏感的或滚动敏感的两个结果集都是双向的,即,您可以将它们向前和向后移动。
您可以使用createStatement()方法,将一个Statement对象创建为返回Scrollable结果集-
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
您可以使用next() 方法向前遍历ResultSet对象的内容,也可以使用forward()方法反向遍历ResultSet对象的内容。
让我们使用CREATE语句在MySQL数据库中创建一个名称为cricketers_data 的表,如下所示-
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语句在cricketers_data 表中插入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');
我们已使用JDBC程序连接到数据库,并将上表的记录检索到ResultSet对象中,并使用next()
和previous()
方法以正向和反向遍历该表。
示例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ScrollableResultset { public static void main(String args[])throws Exception { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建一个Statement对象 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //查询以检索cricketers_data表的内容 String query = "select * from cricketers_Data"; //执行查询 ResultSet rs = stmt.executeQuery(query); //ResultSet的内容正向 System.out.println("ResultSet的内容正向"); while (rs.next()) { System.out.print("id: "+rs.getInt(1)+", "); System.out.print("First name: "+rs.getString(2)+", "); System.out.print("Last name: "+rs.getString(3)+", "); System.out.print("Year of birth: "+rs.getDate(4)+", "); System.out.print("Place of birth: "+rs.getString(5)+", "); System.out.print("Country: "+rs.getString(6)); System.out.println(" "); } System.out.println(" "); //反向的ResultSet的内容 System.out.println("反向的ResultSet的内容"); rs.afterLast(); while (rs.previous()) { System.out.print("id: "+rs.getInt(1)+", "); System.out.print("First name: "+rs.getString(2)+", "); System.out.print("Last name: "+rs.getString(3)+", "); System.out.print("Year of birth: "+rs.getDate(4)+", "); System.out.print("Place of birth: "+rs.getString(5)+", "); System.out.print("Country: "+rs.getString(6)); System.out.println(" "); } } }
输出结果
Connection established...... ResultSet的内容正向 id: 1, First name: Shikhar, Last name: Dhawan, Year of birth: 1981-12-05, Place of birth: Delhi, Country: India id: 2, First name: Jonathan, Last name: Trott, Year of birth: 1981-04-22, Place of birth: CapeTown, Country: SouthAfrica id: 3, First name: Kumara, Last name: Sangakkara, Year of birth: 1977-10-27, Place of birth: Matale, Country: Srilanka id: 4, First name: Virat, Last name: Kohli, Year of birth: 1988-11-05, Place of birth: Mumbai, Country: India id: 5, First name: Rohit, Last name: Sharma, Year of birth: 1987-04-30, Place of birth: Nagpur, Country: India id: 6, First name: Ravindra, Last name: Jadeja, Year of birth: 1988-12-06, Place of birth: Nagpur, Country: India id: 7, First name: James, Last name: Anderson, Year of birth: 1982-06-30, Place of birth: Burnley, Country: England 反向的ResultSet的内容 id: 7, First name: James, Last name: Anderson, Year of birth: 1982-06-30, Place of birth: Burnley, Country: England id: 6, First name: Ravindra, Last name: Jadeja, Year of birth: 1988-12-06, Place of birth: Nagpur, Country: India id: 5, First name: Rohit, Last name: Sharma, Year of birth: 1987-04-30, Place of birth: Nagpur, Country: India id: 4, First name: Virat, Last name: Kohli, Year of birth: 1988-11-05, Place of birth: Mumbai, Country: India id: 3, First name: Kumara, Last name: Sangakkara, Year of birth: 1977-10-27, Place of birth: Matale, Country: Srilanka id: 2, First name: Jonathan, Last name: Trott, Year of birth: 1981-04-22, Place of birth: CapeTown, Country: SouthAfrica id: 1, First name: Shikhar, Last name: Dhawan, Year of birth: 1981-12-05, Place of birth: Delhi, Country: India