JDBC 中有多少种类型的结果集?它们是什么?
jdbcjava 8mysqlmysqli database更新于 2025/5/28 1:16:17
结果集有两种类型,即只向前和双向。
只向前的结果集:光标只向一个方向移动的 ResultSet 对象称为只向前的结果集。默认情况下,JDBC 结果集是只向前的结果集。
您可以使用 ResultSet 接口的 next() 方法移动只向前的 ResultSets 的光标。它将指针从当前位置移动到下一行。此方法返回一个布尔值。如果其当前位置旁边没有行,则返回 false,否则返回 true。
因此,在 while 循环中使用此方法可以迭代 ResultSet 对象的内容。
while(rs.next()){
}
示例
假设我们有一个名为 dataset 的表,其内容如下所示:
+--------------+-----------+ | mobile_brand | unit_sale | +--------------+-----------+ | Iphone | 3000 | | Samsung | 4000 | | Nokia | 5000 | | Vivo | 1500 | +--------------+-----------+
以下示例检索 Dataset 表的所有记录并打印结果:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class RetrievingData {
public static void main(String args[]) throws Exception {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/TestDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating a Statement object
Statement stmt = con.createStatement();
//Retrieving the data
ResultSet rs = stmt.executeQuery("select * from Dataset");
System.out.println("Contents of the table");
while(rs.next()) {
System.out.print("Brand: "+rs.getString("Mobile_Brand")+", ");
System.out.print("Sale: "+rs.getString("Unit_Sale"));
System.out.println("");
}
}
}
输出
Connection established...... Contents of the table Brand: Iphone, Sale: 3000 Brand: Samsung, Sale: 4000 Brand: Nokia, Sale: 5000 Brand: Vivo, Sale: 1500
双向 ResultSet:双向 ResultSet 对象是光标可以向前和向后移动的对象。
Connection 接口的 createStatement() 方法有一个变体,它接受两个整数值,分别表示结果集类型和并发类型。
Statement createStatement(int resultSetType, int resultSetConcurrency)
要创建双向结果集,您需要将类型作为 ResultSet.TYPE_SCROLL_SENSITIVE 或 ResultSet.TYPE_SCROLL_INSENSITIVE 以及并发性传递给此方法,如下所示:
//创建 Statement 对象 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
示例
以下示例演示了双向 ResultSet 的创建。在这里,我们尝试创建一个双向 ResultSet 对象,该对象从表名数据集中检索数据,并尝试使用 previous() 方法从最后一行到第一行打印数据集表中的行。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class BidirectionalResultSet {
public static void main(String args[]) throws Exception {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/TestDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating a Statement object
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//Retrieving the data
ResultSet rs = stmt.executeQuery("select * from Dataset");
rs.afterLast();
System.out.println("Contents of the table");
while(rs.previous()) {
System.out.print("Brand: "+rs.getString("Mobile_Brand")+", ");
System.out.print("Sale: "+rs.getString("Unit_Sale"));
System.out.println("");
}
}
}
输出
Connection established...... Contents of the table Brand: Vivo, Sale: 1500 Brand: Nokia, Sale: 5000 Brand: Samsung, Sale: 4000 Brand: IPhone, Sale: 3000
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

