如何使用 JDBC 将 ResultSet 的指针移至默认位置?


ResultSet 接口的 beofreFirst() 方法将游标/指针移至其默认位置,即第一个记录之前。

rs.beforeFirst();

假设我们有一个名为 cricketers_data 的表,其中包含 6 条记录,如下所示

+----+------------+------------+---------------+----------------+-------------+
| ID | First_Name | Last_Name  | Year_Of_Birth | Place_Of_Birth | Country     |
+----+------------+------------+---------------+----------------+-------------+
| 1 | Shikhar     | Dhawan     | 1981-12-05    | Delhi          | India       |
| 2 | Jonathan    | Trott      | 1981-04-22    | CapeTown       | SouthAfrica |
| 3 | Lumara      | Sangakkara | 1977-10-27    | Matale         | Srilanka    |
| 4 | Virat       | Kohli      | 1988-11-05    | Delhi          | India       |
| 5 | Rohit       | Sharma     | 1987-04-30    | Nagpur         | India       |
| 6 | Ravindra    | Jadeja     | 1988-12-06    | Nagpur         | India       |
+----+------------+------------+---------------+----------------+-------------+

以下 JDBC 程序演示了如何将 ResultSet 指针移至默认位置,即第一行之前。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ResultSet_DefaultPosition {
   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://127.0.0.1/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
      //Query to retrieve the contents of the employee_data table
      String query = "select * from Cricketers_Dataa";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      rs.absolute(4);
      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(" ");
      //Contents of the current row
      rs.beforeFirst();
      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(" ");
      }
   }
}

输出

Connection established......
id: 4, First name: Virat, Last name: Kohli, Year of birth: 1988-11-05, Place of birth: Delhi, Country: India
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: Lumara, 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: Delhi, 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: Burnely, Country: England

更新于:30-7 月 2019

517 次查看

开启你的 职业生涯

通过完成该课程获取认证

开始
广告