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://127.0.0.1/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:\>
广告