Java & MySQL - ResultSet 導航



ResultSet 介面中包含幾種移動游標的方法,包括:

序號 方法 & 描述
1 public void beforeFirst() throws SQLException

將游標移動到第一行之前。

2 public void afterLast() throws SQLException

將游標移動到最後一行之後。

3 public boolean first() throws SQLException

將游標移動到第一行。

4 public void last() throws SQLException

將游標移動到最後一行。

5 public boolean absolute(int row) throws SQLException

將游標移動到指定行。

6 public boolean relative(int row) throws SQLException

將游標從當前位置向前或向後移動指定行數。

7 public boolean previous() throws SQLException

將游標移動到上一行。如果上一行超出結果集,則此方法返回 false。

8 public boolean next() throws SQLException

將游標移動到下一行。如果結果集中沒有更多行,則此方法返回 false。

9 public int getRow() throws SQLException

返回游標指向的行號。

10 public void moveToInsertRow() throws SQLException

將游標移動到結果集中的一個特殊行,該行可用於將新行插入資料庫。當前游標位置會被記住。

11 public void moveToCurrentRow() throws SQLException

如果游標當前位於插入行,則將游標移回當前行;否則,此方法不執行任何操作。

以下示例使用了前面描述的一些導航方法。

此示例程式碼是根據前面章節中完成的環境和資料庫設定編寫的。

將以下示例複製並貼上到 TestApplication.java 中,然後編譯並執行:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestApplication {
   static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
   static final String USER = "guest";
   static final String PASS = "guest123";
   static final String QUERY = "SELECT id, first, last, age FROM Employees";

   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         Statement stmt = conn.createStatement(
         ResultSet.TYPE_SCROLL_INSENSITIVE,
         ResultSet.CONCUR_READ_ONLY);
         ResultSet rs = stmt.executeQuery(QUERY);
      ) {		
         // Move cursor to the last row.
         System.out.println("Moving cursor to the last...");
         rs.last();

         // Extract data from result set
         System.out.println("Displaying record...");
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         // Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);

         // Move cursor to the first row.
         System.out.println("Moving cursor to the first row...");
         rs.first();

         // Extract data from result set
         System.out.println("Displaying record...");
         // Retrieve by column name
         id  = rs.getInt("id");
         age = rs.getInt("age");
         first = rs.getString("first");
         last = rs.getString("last");

         // Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
         // Move cursor to the first row.

         System.out.println("Moving cursor to the next row...");
         rs.next();

         // Extract data from result set
         System.out.println("Displaying record...");
         id  = rs.getInt("id");
         age = rs.getInt("age");
         first = rs.getString("first");
         last = rs.getString("last");

         // Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);		

      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

現在,讓我們按如下方式編譯上述示例:

C:\>javac TestApplication.java
C:\>

執行TestApplication時,將產生以下結果:

C:\>java TestApplication
Moving cursor to the last...
Displaying record...
ID: 103, Age: 30, First: Sumit, Last: Mittal
Moving cursor to the first row...
Displaying record...
ID: 100, Age: 18, First: Zara, Last: Ali
Moving cursor to the next row...
Displaying record...
ID: 101, Age: 25, First: Mehnaz, Last: Fatma
C:\>
廣告
© . All rights reserved.