如何使用JDBC查找ResultSet对象的当前行?
ResultSet类的getRow()方法返回当前实例中ResultSet指针所在的行号。
假设我们有一个名为cricketers_data的表,其中包含6条记录,如下所示:
+------------+------------+---------------+----------------+-------------+| First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country |
+------------+------------+---------------+----------------+-------------+
| Shikhar | Dhawan | 1981-12-05 | Delhi | India |
| Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica |
| Lumara | Sangakkara | 1977-10-27 | Matale | Srilanka |
| Virat | Kohli | 1987-04-30 | Delhi | India |
| Rohit | Sharma | 1987-04-30 | Nagpur | India |
| Ravindra | Jamnagar | 1988-12-06 | NULL | India |
+------------+------------+---------------+----------------+-------------+
接下来的JDBC程序建立与数据库的连接,将cricketers_data表的内容检索到ResultSet对象中,并打印ResultSet指针的当前位置。
import java.sql.Connection;import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ResultSet_CurrentRow {
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();
//查询以获取employee_data表的内容
String query = "select * from cricketers_data";
//执行查询
ResultSet rs = stmt.executeQuery(query);
//将ResultSet指针移动到特定位置
rs.absolute(2);
//获取ResultSet对象的当前行
int current_row = rs.getRow();
System.out.println(current_row);
}
}
输出结果
Connection established......2
以上是 如何使用JDBC查找ResultSet对象的当前行? 的全部内容, 来源链接: utcz.com/z/321668.html