JDBC - 檢視結果集示例



以下示例使用了結果集章節中描述的一些**getInt** 和**getString** 方法。此示例與導航結果集部分中解釋的先前示例非常相似。

此示例程式碼是根據前面章節中完成的環境和資料庫設定編寫的。

逐行檢視 ResultSet 中的記錄示例

在此示例中,我們有四個靜態字串,包含資料庫連線 URL、使用者名稱、密碼和 SELECT 查詢。現在使用 DriverManager.getConnection() 方法,我們準備了一個數據庫連線。連線準備就緒後,我們使用 connection.createStatement() 方法建立了一個 Statement 物件。在建立 Statement 物件時,我們使用了 ResultSet 型別 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY,然後使用 statement.executeQuery() 執行 SELECT 查詢並將結果儲存在結果集中。

在第一步中,我們使用 ResultSet.last() 方法將結果集遊標移動到最後一行,並使用 getInt() 和 getString() 方法列印最後一條記錄的詳細資訊。getInt() 方法用於獲取數字欄位(如 id、age),getString() 用於獲取 varchar 欄位(如名字、姓氏)。在第二步中,我們使用 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
C:\>

在迴圈中檢視 ResultSet 中的記錄示例

在此示例中,我們有四個靜態字串,包含資料庫連線 URL、使用者名稱、密碼和 SELECT 查詢。現在使用 DriverManager.getConnection() 方法,我們準備了一個數據庫連線。連線準備就緒後,我們使用 connection.createStatement() 方法建立了一個 Statement 物件。在建立 Statement 物件時,我們使用了 ResultSet 型別 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY,然後使用 statement.executeQuery() 執行 SELECT 查詢並將結果儲存在結果集中。

在第一步中,我們使用 ResultSet.next() 在 while 迴圈中將結果集遊標移動到下一條記錄並檢查記錄是否存在。在 while 迴圈中,我們使用了 getInt() 和 getString(),其中引數是列名以獲取相應的值。列印完所有記錄後,我們使用 ResultSet.first() 方法將結果集遊標移動到第一條記錄。在這裡,我們使用了 getInt() 和 getString(),其中引數是從 1 開始的列索引以獲取相應的值並列印該記錄。

將以下示例複製並貼上到 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 , Dept 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);
      ) {		
         // Extract data from result set
         System.out.println("Displaying record by column name ...");
         //Retrieve by column name
         while(rs.next()){
            int id  = rs.getInt("StudentID");
            String first = rs.getString("FirstName");
            String last = rs.getString("LastName");
            String dept = rs.getString("Dept");

            // Display values
            System.out.print("StudentID: " + id);
            System.out.print(", First: " + first);
            System.out.print(", Last: " + last);
            System.out.println(", Dept: " + dept);
         }

         // Move cursor to the first row.
         System.out.println("Moving cursor to the first row...");
         rs.first();

         System.out.println("Displaying record by column index...");
         // Retrieve by column index
         int id  = rs.getInt(1);
         String first = rs.getString(2);
         String last = rs.getString(3);
         String dept = rs.getString(4);

         // Display values
         System.out.print("StudentID: " + id);
         System.out.print(", First: " + first);
         System.out.print(", Last: " + last);
         System.out.println(", Dept: " + dept);

      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

輸出

現在讓我們按如下方式編譯上述示例:

C:\>javac ResultSetExample.java
C:\>

執行**ResultSetExample** 時,會產生以下結果:

C:\>java ResultSetExample
Displaying record by column name ...
StudentID: 1000, First: Bonny, Last: Agarwal, Dept: Mathematics
StudentID: 1001, First: Amit, Last: Pandey, Dept: Physics
StudentID: 1002, First: Shefali, Last: Kumar, Dept: English
StudentID: 1004, First: Mohammed, Last: Ali, Dept: Mathematics
StudentID: 1005, First: Kishore, Last: Kumar, Dept: Biology
StudentID: 1006, First: Ganesh, Last: Khan, Dept: English
Moving cursor to the first row...
Displaying record by column index...
StudentID: 1000, First: Bonny, Last: Agarwal, Dept: Mathematics

C:\>
jdbc-result-sets.htm
廣告