Java 和 MySQL - 导航结果集


ResultSet 接口中有多种涉及移动光标的方法,包括 -

序列号 方法与说明
1 public void beforeFirst() 抛出 SQLException

将光标移动到第一行之前。

2 public void afterLast() 抛出 SQLException

将光标移动到最后一行之后。

3 public boolean first() 抛出 SQLException

将光标移至第一行。

4 public void last() 抛出 SQLException

将光标移动到最后一行。

5 public boolean Absolute(int row) 抛出 SQLException

将光标移动到指定行。

6 public booleanrelative(int row) 抛出 SQLException

将光标从当前指向的位置向前或向后移动给定的行数。

7 public boolean previous() 抛出 SQLException

将光标移动到上一行。如果前一行不在结果集中,则此方法返回 false。

8 public boolean next() 抛出 SQLException

将光标移动到下一行。如果结果集中没有更多行,则此方法返回 false。

9 public int getRow() 抛出 SQLException

返回光标指向的行号。

10 public void moveToInsertRow() 抛出 SQLException

将游标移动到结果集中的特殊行,可用于将新行插入数据库。当前光标位置被记住。

11 public void moveToCurrentRow() 抛出 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://localhost/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:\>