- JDBC 教程
- JDBC - 首页
- JDBC - 简介
- JDBC - SQL 语法
- JDBC - 环境配置
- JDBC - 示例代码
- JDBC - 驱动程序类型
- JDBC - 连接
- JDBC - 语句
- JDBC - 结果集
- JDBC - 数据类型
- JDBC - 事务
- JDBC - 异常
- JDBC - 批处理
- JDBC - 存储过程
- JDBC - 数据流
- JDBC - RowSet
- JDBC - 复制数据库
- JDBC - ACID 属性
- JDBC - 连接池
- JDBC 示例
- JDBC - 创建数据库
- JDBC - 选择数据库
- JDBC - 删除数据库
- JDBC - 创建表
- JDBC - 删除表
- JDBC - 插入记录
- JDBC - 查询记录
- JDBC - 更新记录
- JDBC - 删除记录
- JDBC - WHERE 子句
- JDBC - LIKE 子句
- JDBC - 数据排序
- JDBC 有用资源
- JDBC - 常见问题解答
- JDBC - 快速指南
- JDBC - 有用资源
- JDBC - 讨论
- 有用 - Java 教程
JDBC - 结果集导航示例
以下是使用结果集教程中描述的一些导航方法的示例。
此示例代码是基于前面章节中完成的环境和数据库设置编写的。
在结果集中导航到第一条、最后一条和下一条记录的示例
在这个例子中,我们有四个静态字符串,包含数据库连接 URL、用户名、密码和 SELECT 查询。现在使用 DriverManager.getConnection() 方法,我们准备了一个数据库连接。连接准备就绪后,我们使用 connection.createStatement() 方法创建了一个 Statement 对象。在创建 Statement 对象时,我们使用了 ResultSet 类型 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY,然后使用 statement.executeQuery() 执行 SELECT 查询,并将结果存储在结果集中。
第一步,我们使用 ResultSet.last() 方法将结果集游标移动到最后一行,并打印最后一条记录。第二步,我们使用 ResultSet.first() 方法将游标移动到第一行,并打印第一条记录。最后一步,我们使用 ResultSet.next() 方法将游标移动到下一条记录,并打印该记录。
复制并粘贴以下示例到 ResultSetExample.java 中,编译并运行如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSetExample {
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 ResultSetExample.java C:\>
运行 **ResultSetExample** 时,会产生以下结果:
C:\>java ResultSetExample 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 Goodbye! C:\>
在结果集中导航到特定记录的示例
在这个例子中,我们有四个静态字符串,包含数据库连接 URL、用户名、密码和 SELECT 查询。现在使用 DriverManager.getConnection() 方法,我们准备了一个数据库连接。连接准备就绪后,我们使用 connection.createStatement() 方法创建了一个 Statement 对象。在创建 Statement 对象时,我们使用了 ResultSet 类型 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY,然后使用 statement.executeQuery() 执行 SELECT 查询,并将结果存储在结果集中。
第一步,我们使用 ResultSet.absolute() 方法将结果集游标移动到特定行,并打印该记录。第二步,我们使用 ResultSet.first() 方法将游标移动到第一行,并打印第一条记录。最后一步,我们使用 ResultSet.next() 方法将游标移动到下一条记录,并打印该记录。
复制并粘贴以下示例到 ResultSetExample.java 中,编译并运行如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSetExample {
static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "SELECT StudentID, FirstName, LastName FROM Students";
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 third row.
System.out.println("Moving cursor to the 3rd row...");
rs.absolute(3);
// Extract data from result set
System.out.println("Displaying record of 3rd row...");
//Retrieve by column name
int id = rs.getInt("StudentID");
String first = rs.getString("FirstName");
String last = rs.getString("LastName");
// Display values
System.out.print("StudentID: " + id);
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("StudentID");
first = rs.getString("FirstName");
last = rs.getString("LastName");
// Display values
System.out.print("ID: " + id);
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("StudentID");
first = rs.getString("FirstName");
last = rs.getString("LastName");
// Display values
System.out.print("ID: " + id);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
输出
现在让我们编译上面的示例,如下所示:
C:\>javac ResultSetExample.java C:\>
运行 **ResultSetExample** 时,会产生以下结果:
C:\>java ResultSetExample Moving cursor to the 3rd row... Displaying record of 3rd... StudentID: 1002, First: Shefali, Last: Kumar Moving cursor to the first row... Displaying record... ID: 1000, First: Bonny, Last: Agarwal Moving cursor to the next row... Displaying record... ID: 1001, First: Amit, Last: Pandey C:\>