Java Mysql 简明教程

Java & MySQL - Navigating a ResultSet

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

S.N.

Methods & Description

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://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:\>