Java 和 MySQL - 浏览 ResultSet
ResultSet 接口中有几种方法涉及移动光标,包括 −
| S.N. | 方法 &描述 |
|---|---|
| 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 related(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) {
// 打开连接
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();
// 从结果集中提取数据
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");
// 显示值
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();
// 从结果集中提取数据
System.out.println("Displaying record...");
// 根据列名检索
id = rs.getInt("id");
age = rs.getInt("age");
first = rs.getString("first");
last = rs.getString("last");
// 显示值
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();
// 从结果集中提取数据
System.out.println("Displaying record...");
id = rs.getInt("id");
age = rs.getInt("age");
first = rs.getString("first");
last = rs.getString("last");
// 显示值
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:\>

