JDBC - RowSet



RowSet 是一個介面,類似於 ResultSet,位於 java.sql 下的 javax.sql.rowset 包中。它繼承自 ResultSet,並且比 ResultSet 具有更多功能。RowSet 可以是可滾動的和可更新的。某些資料庫管理系統不支援 ResultSet 的可滾動性和可更新性。在這種情況下,必須使用 RowSet。此外,RowSet 具有 Java Bean 功能。它們具有屬性。可以使用 setter 和 getter 方法設定或檢索屬性。RowSet 使用事件模型,在發生某些事件時會傳播事件。

事件分為以下三種類型:

  • 更新、插入或刪除一行

  • 游標移動

  • RowSet 內容發生更改

RowSet 介面型別

RowSet 介面有 5 個主要的子介面。

  • JdbcRowSet

  • CachedRowSet

  • WebRowSet

  • JoinRowSet

  • FilteredRowSet

示例:使用 RowSet 執行基本的 CRUD 操作

在這個例子中,我們有三個靜態字串包含資料庫連線 URL、使用者名稱和密碼。現在使用 RowSetProvider.newFactory().createJdbcRowSet() 方法,我們準備了一個 JdbcRowSet 物件作為 rowSet。在 rowSet 中,我們設定了 URL、使用者名稱、密碼和一個 SQL 命令,用於從 employees 表中獲取員工詳細資訊。使用 rowSet.execute(),查詢被執行,記錄儲存在 rowSet 例項中。

使用 rowSet.next() 方法,我們檢查是否還有更多記錄,然後使用 getInt() 和 getString() 方法獲取所需的值,並在 while 迴圈中列印它們以列印所有員工。

使用 rowSet.absolute(3) 方法,我們移動到第三條記錄,並使用 rowSet.updateInt() 將年齡更新為 30。接下來,使用 rowSet.updateRow() 更新資料庫中的完整記錄。現在使用 rowSet.last() 方法,我們移動到最後一條記錄,並使用 rowSet.deleteRow() 方法刪除該記錄。現在為了驗證更改,我們設定命令來選擇員工的詳細資訊並執行它。最後,我們再次列印所有員工的記錄。

將以下示例複製並貼上到 RowSetExample.java 中,編譯並執行如下:

import javax.sql.rowset.*;
import java.sql.*;

// This class demonstrates use of basic functionality of rowsets
public class RowSetExample {

   static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
   static final String USER = "root";
   static final String PASS = "guest123";

   public static void main(String args[])throws SQLException {
      JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();  
      rowSet.setUrl(DB_URL);  
      rowSet.setUsername(USER);  
      rowSet.setPassword(PASS);  
      rowSet.setCommand("select id, age, first, last from employees");  
      rowSet.execute();  

      while (rowSet.next()) {  
         System.out.print("Id: " + rowSet.getInt(“id”));
         System.out.print(" Age: " + rowSet.getInt("age"));
         System.out.print(" First: " + rowSet.getString("first"));
         System.out.println(" Last: " + rowSet.getString("last"));
      }  

      System.out.println("---------------------------------------");
      // Position the cursor to the 3rd row
      rowSet.absolute(3);
      rowSet.updateInt("age",20 );
      rowSet.updateRow();

      //position the cursor to the last row
      rowSet.last();
      rowSet.deleteRow();

      // After updating the 3rd row and deleting the last row, doing a select to view updated records.
      rowSet.setCommand("select id, age, first, last from employees");  
      rowSet.execute();

      while (rowSet.next()) {  
         System.out.print("Id: " + rowSet.getInt(“id”));
         System.out.print(" Age: " + rowSet.getInt("age"));
         System.out.print(" First: " + rowSet.getString("first"));
         System.out.println(" Last: " + rowSet.getString("last"));
      }  
   }
}

輸出

現在讓我們編譯上面的示例,如下所示:

C:\>javac RowSetExample.java
C:\>

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

C:\>java RowSetExample
Id: 1 Age: 18 First: Zara Last: Ali
Id: 2 Age: 25 First: Mahnaz Last: Fatma
Id: 3 Age: 30 First: Zaid Last: Khan
Id: 4 Age: 28 First: Sumit Last: Mittal
Id: 7 Age: 20 First: Rita Last: Tez
Id: 8 Age: 20 First: Sita Last: Singh
Id: 21 Age: 35 First: Jeevan Last: Rao
Id: 22 Age: 40 First: Aditya Last: Chaube
Id: 25 Age: 35 First: Jeevan Last: Rao
Id: 26 Age: 35 First: Aditya Last: Chaube
Id: 34 Age: 45 First: Ahmed Last: Ali
Id: 35 Age: 50 First: Raksha Last: Agarwal
Id: 36 Age: 50 First: Sankalp Last: Hawladar
Id: 37 Age: 50 First: Anand Last: Roy
---------------------------------------
Id: 1 Age: 18 First: Zara Last: Ali
Id: 2 Age: 25 First: Mahnaz Last: Fatma
Id: 3 Age: 20 First: Zaid Last: Khan
Id: 4 Age: 28 First: Sumit Last: Mittal
Id: 7 Age: 20 First: Rita Last: Tez
Id: 8 Age: 20 First: Sita Last: Singh
Id: 21 Age: 35 First: Jeevan Last: Rao
Id: 22 Age: 40 First: Aditya Last: Chaube
Id: 25 Age: 35 First: Jeevan Last: Rao
Id: 26 Age: 35 First: Aditya Last: Chaube
Id: 34 Age: 45 First: Ahmed Last: Ali
Id: 35 Age: 50 First: Raksha Last: Agarwal
Id: 36 Age: 50 First: Sankalp Last: Hawladar

C:\>

RowSet 操作中的事件處理

有一個介面 RowSetListener 需要透過 addRowSetListener 新增到 JdbcRowSet。RowSetListener 中有三個需要實現的方法。

public void cursorMoved(RowSetEvent event)

此方法在 RowSet 游標移動時觸發。

public void rowChanged(RowSetEvent event)

當 RowSet 中的一行發生更改時,此方法被觸發。

public void rowSetChanged(RowSetEvent event); 

當 RowSet 的整個內容發生更改時,此方法被觸發。

示例:使用監聽器處理 RowSet 操作中的事件

在這個例子中,我們有三個靜態字串包含資料庫連線 URL、使用者名稱和密碼。現在使用 RowSetProvider.newFactory().createJdbcRowSet() 方法,我們準備了一個 JdbcRowSet 物件作為 rowSet。在 rowSet 中,我們設定了 URL、使用者名稱、密碼和一個 SQL 命令,用於從 employees 表中獲取員工詳細資訊。使用 rowSet.execute(),查詢被執行,記錄儲存在 rowSet 例項中。

我們定義了一個 RowSetListener 作為 CustomListener,它監聽各種 RowSetEvent。使用 rowSet.addRowSetListener(),此監聽器註冊到 rowSet。

使用 rowSet.next() 方法,我們檢查是否還有更多記錄,然後使用 getInt() 和 getString() 方法獲取所需的值,並在 while 迴圈中列印它們以列印所有員工。

使用 rowSet.absolute(3) 方法,我們移動到第三條記錄,並使用 rowSet.updateInt() 將年齡更新為 30。接下來,使用 rowSet.updateRow() 更新資料庫中的完整記錄。現在當程式執行時,CustomListener 物件監聽各種事件,並相應地在控制檯上列印結果。

將以下示例複製並貼上到 RowSetExample.java 中,編譯並執行如下:

import javax.sql.rowset.*;
import javax.sql.*;
import java.sql.*;

// This class demonsrates use of RowSetEvent and event-handling in RowSet
public class RowSetExample {

   static final String DB_URL = "jdbc:mysql:///TUTORIALSPOINT";
   static final String USER = "root";
   static final String PASS = "guest123";

   public static void main(String args[]) throws SQLException {
      JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();  
      rowSet.setUrl(DB_URL);  
      rowSet.setUsername(USER);  
      rowSet.setPassword(PASS);
      rowSet.setCommand("select id, age, first, last from employees");  
      rowSet.execute();
      rowSet.addRowSetListener(new CustomListener());  

      while (rowSet.next()) {  
         // Generating cursor Moved event        
         System.out.print("Id: " + rowSet.getInt(1));
         System.out.print(" Age: " + rowSet.getInt("age"));
         System.out.print(" First: " + rowSet.getString("first"));       
      } 
      System.out.println(" Last: " + rowSet.getString("last"));
      System.out.println("-------------------------------------------------");
      rowSet.absolute(3);
      rowSet.updateInt("age",30 );
      //Generating row changed event
      rowSet.updateRow();
   }  
}  

class CustomListener implements RowSetListener {  
   public void cursorMoved(RowSetEvent evt) {  
      System.out.println("Cursor Moved...");  
   }  
   public void rowChanged(RowSetEvent evt) {  
      System.out.println("Row Changed...");  
   }  
   public void rowSetChanged(RowSetEvent evt){
      System.out.println("RowSet changed..");
   }
}  

輸出

現在讓我們編譯上面的示例,如下所示:

C:\>javac RowSetExample.java
C:\>

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

Cursor Moved...
Id: 1 Age: 18 First: Zara Last: Ali
Cursor Moved...
Id: 2 Age: 25 First: Mahnaz Last: Fatma
Cursor Moved...
Id: 3 Age: 20 First: Zaid Last: Khan
Cursor Moved...
Id: 4 Age: 28 First: Sumit Last: Mittal
Cursor Moved...
Id: 7 Age: 20 First: Rita Last: Tez
Cursor Moved...
Id: 8 Age: 20 First: Sita Last: Singh
Cursor Moved...
Id: 21 Age: 35 First: Jeevan Last: Rao
Cursor Moved...
Id: 22 Age: 40 First: Aditya Last: Chaube
Cursor Moved...
Id: 25 Age: 35 First: Jeevan Last: Rao
Cursor Moved...
Id: 26 Age: 35 First: Aditya Last: Chaube
Cursor Moved...
Id: 34 Age: 45 First: Ahmed Last: Ali
Cursor Moved...
Id: 35 Age: 50 First: Raksha Last: Agarwal
Cursor Moved...
Id: 36 Age: 50 First: Sankalp Last: Hawladar
Cursor Moved...
-------------------------------------------------
Cursor Moved...
Row Changed...

CachedRowSet

CachedRowSet 將資料儲存在快取到記憶體中的行中。下面的示例顯示瞭如何在 CachedRowSet 上使用 setPageSize。頁面大小是每頁顯示的行數,例如,在 GUI 上。

CachedRowSet 是一個斷開連線的 RowSet。這意味著它在更新或從資料庫檢索時獲取資料庫連線。其他時間,資料庫連線斷開。

示例:使用 CacheRowSet 操作

在這個例子中,使用 RowSetProvider.newFactory().createCachedRowSet() 方法,我們準備了一個 CachedRowSet 物件作為 rowSet。在 rowSet 中,我們設定了 URL、使用者名稱、密碼和一個 SQL 命令,用於從 employees 表中獲取員工詳細資訊。使用 setPageSize(),我們將 CachedRowSet 物件 rowSet 的頁面大小設定為 2 條記錄。使用 rowSet.execute(),查詢被執行,記錄儲存在 rowSet 例項中。

使用 rowSet.nextPage() 方法,我們檢查下一頁中是否存在更多記錄並列印頁碼。現在使用 rowSet.next(),我們檢查頁面中可用的記錄,並使用 getInt() 和 getString() 方法獲取所需的值,並在 while 迴圈中列印它們以按頁列印所有員工。

將以下示例複製並貼上到 RowSetExample.java 中,編譯並執行如下:

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.Time;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

// This class demonstrates use of CachedRowSet
public class RowSetExample {
   public static void main(String args[]) throws Exception {
      RowSetFactory factory = RowSetProvider.newFactory();
      CachedRowSet rowSet = factory.createCachedRowSet();
      //Setting the URL
      String mysqlUrl = "jdbc:mysql:///TUTORIALSPOINT";
      rowSet.setUrl(mysqlUrl);
      //Setting the user name
      rowSet.setUsername("guest");
      //Setting the password
      rowSet.setPassword("guest123");
      //Setting the query/command
      rowSet.setCommand("select * from employees");
      rowSet.setPageSize(2);
      rowSet.execute();   
      System.out.println("Contents of the row set");
      int i = 1;
      while(rowSet.nextPage()) {
         System.out.println(" Page No: " + i);
         System.out.println("-------------------------------------");
         while(rowSet.next()){
            System.out.print("ID : "+rowSet.getInt("id")+", ");
            System.out.print("Age : "+rowSet.getInt("age")+", ");
            System.out.print("First : "+rowSet.getString("first")+", ");
            System.out.println("Last : "+rowSet.getString("last")+", ");
         }
         System.out.println("------------------------------------- End of page");
         i++;
      }
   }
}

輸出

現在讓我們編譯上面的示例,如下所示:

C:\>javac RowSetExample.java
C:\>

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

Contents of the row set
 Page No: 1
-------------------------------------
ID : 3, Age : 20, First : Zaid, Last : Khan, 
ID : 4, Age : 28, First : Sumit, Last : Mittal, 
------------------------------------- End of page
 Page No: 2
-------------------------------------
ID : 7, Age : 20, First : Rita, Last : Tez, 
ID : 8, Age : 20, First : Sita, Last : Singh, 
------------------------------------- End of page
 Page No: 3
-------------------------------------
ID : 21, Age : 35, First : Jeevan, Last : Rao, 
ID : 22, Age : 40, First : Aditya, Last : Chaube, 
------------------------------------- End of page
 Page No: 4
-------------------------------------
ID : 25, Age : 35, First : Jeevan, Last : Rao, 
ID : 26, Age : 35, First : Aditya, Last : Chaube, 
------------------------------------- End of page
 Page No: 5
-------------------------------------
ID : 34, Age : 45, First : Ahmed, Last : Ali, 
ID : 35, Age : 50, First : Raksha, Last : Agarwal, 
------------------------------------- End of page
廣告
© . All rights reserved.