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