如何在 JDBC 中將資料插入 CachedRowSet?解釋一下?


CachedRowSet 是斷開連線的行集的基本實現。它連線到資料來源,從中讀取資料,與資料來源斷開連線,並處理檢索到的資料,重新連線到資料來源並寫入修改。

建立 CachedRowSet

您可以使用 RowSetFactory 的 createCachedRowSet() 方法建立一個 Cached RowSet 物件。

您可以使用 RowSetProvider 方法的 newfactory() 方法建立一個 RowSetFactory 物件。

使用上述方法建立 CachedRowSet 物件,如下所示:

//Creating the RowSet object
RowSetFactory factory = RowSetProvider.newFactory();
CachedRowSet rowSet = factory.createCachedRowSet();

連線到資料來源

建立 RowSet 物件後,您需要將其連線到所需的資料來源。

您可以透過為屬性設定值(例如使用者名稱、密碼、URL 和資料來源名稱)來連線到資料來源,例如:

//Setting the URL
String mysqlUrl = "jdbc:mysql:///SampleDB";
rowSet.setUrl(mysqlUrl);

//Setting the user name
rowSet.setUsername("root");

//Setting the password
rowSet.setPassword("password");

準備和執行命令語句

快取的行集有一個命令屬性,您可以將查詢傳遞給它。使用 setCommand() 方法將所需的查詢設定為此屬性。

rowSet.setCommand("select * from ProductSales");

您可以使用 execute() 方法執行 RowSet 物件持有的查詢。

rowSet.execute();

在 CachedRowSet 中插入一行

  • CachedRowSet 介面的 moveToInsertRow() 方法將游標導航到您需要插入下一條記錄的位置。

    因此,使用此方法將游標移動到適當的位置以插入一行。

  • CachedRowSet 介面的 updateXXX() 方法允許您將值插入/更新到 RowSet 物件中。

    使用這些方法向新行新增值,例如,如果您需要在第 1 列插入一個整數值,並在第 2 列插入一個字串值,您可以使用 updateInt() 和 updateString() 方法執行此操作,如下所示:

rowSet.updateInt(1, integerValue);
rowSet.updateString(2, "stringValue");
  • insertRow() 方法將行插入到 CachedRowSet 以及表中。

    因此,使用此方法將上面建立的行插入到 CachedRowSet 物件和表中。

示例

假設我們在資料庫中有一個名為 ProductSales 的表,其內容如下:

+----+-------------+--------------+--------------+--------------+-------+----------------+
| ID | ProductName | CustomerName | DispatchDate | DeliveryTime | Price |Location        |
+----+-------------+--------------+--------------+--------------+-------+----------------+
| 1  | Key-Board   | Raja         | 2019-09-01   | 05:30:00     | 7000  |Hyderabad       |
| 2  | Earphones   | Roja         | 2019-05-01   | 05:30:00     | 2000  |Vishakhapatnam  |
| 3  | Mouse       | Puja         | 2019-03-01   | 05:29:59     | 3000  |Vijayawada      |
| 4  | Mobile      | Vanaja       | 2019-03-01   | 04:40:52     | 9000  |Chennai         |
| 5  | Headset     | Jalaja       | 2019-04-06   | 18:38:59     | 6000  |Goa             |
+----+-------------+--------------+--------------+--------------+-------+----------------+

以下示例將上述表的全部內容檢索到 CachedRowSet 物件中,並向其中插入一條新記錄。

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;
public class CachedRowSetExample {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Creating the RowSet object
      RowSetFactory factory = RowSetProvider.newFactory();
      CachedRowSet rowSet = factory.createCachedRowSet();
      //Setting the URL
      String mysqlUrl = "jdbc:mysql:///SampleDB";
      rowSet.setUrl(mysqlUrl);
      //Setting the user name
      rowSet.setUsername("root");
      //Setting the password
      rowSet.setPassword("password");
      //Setting the query/command
      rowSet.setCommand("select * from ProductSales");
      rowSet.execute();
      System.out.println("Contents of the row set");
      while(rowSet.next()) {
         System.out.print("ID: "+rowSet.getInt("ID")+", ");
         System.out.print("Product Name: "+rowSet.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", ");
         System.out.print("Dispatch Date: "+rowSet.getDate("DispatchDate")+", ");
         System.out.print("Delivery Time: "+rowSet.getTime("DeliveryTime"));
         System.out.print("Price: "+rowSet.getString("Price")+", ");
         System.out.print("Location: "+rowSet.getString("Location"));
         System.out.println("");
      }
      //Inserting data into RowSet object
      rowSet.moveToInsertRow();
      rowSet.updateInt(1, 6);
      rowSet.updateString(2, "Laptop");
      rowSet.updateString(3, "Jagadeesh");
      rowSet.updateDate(4, new Date(1551899399000L));
      rowSet.updateTime(5, new Time(1551899399000L));
      rowSet.updateInt(6, 50000);
      rowSet.updateString(7, "Mumbai");
      rowSet.insertRow();
      rowSet.moveToCurrentRow();
      System.out.println("");
      System.out.println("Contents of the row set after inserting a new row: ");
      System.out.println("");
      rowSet.beforeFirst();
      while(rowSet.next()) {
         System.out.print("ID: "+rowSet.getInt("ID")+", ");
         System.out.print("Product Name: "+rowSet.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", ");
         System.out.print("Dispatch Date: "+rowSet.getDate("DispatchDate")+", ");
         System.out.print("Delivery Time: "+rowSet.getTime("DeliveryTime"));
         System.out.print("Price: "+rowSet.getString("Price")+", ");
         System.out.print("Location: "+rowSet.getString("Location"));
         System.out.println("");
      }
   }
}

輸出

Contents of the row set
ID: 1, Product Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 05:30:00Price: 7000, Location: Hyderabad
ID: 2, Product Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 05:30:00Price: 2000, Location: Vishakhapatnam
ID: 3, Product Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 05:29:59Price: 3000, Location: Vijayawada
ID: 4, Product Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 04:40:52Price: 9000, Location: Chennai
ID: 5, Product Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 18:38:59Price: 6000, Location: Goa

Contents of the table after inserting a new row:

ID: 1, Product Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 05:30:00Price: 7000, Location: Hyderabad
ID: 2, Product Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 05:30:00Price: 2000, Location: Vishakhapatnam
ID: 3, Product Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 05:29:59Price: 3000, Location: Vijayawada
ID: 4, Product Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 04:40:52Price: 9000, Location: Chennai
ID: 5, Product Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 18:38:59Price: 6000, Location: Goa
ID: 6, Product Name: Laptop, Customer Name: Jagadeesh, Dispatch Date: 1970-01-19, Delivery Time: 04:34:59Price: 50000, Location: Mumbai

更新於: 2019-07-30

566 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告