如何在JDBC中更新CachedRowSet物件中某一行的列?


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

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

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

更新行的特定列

CachedRowSet介面的**updateXXX()**方法允許您更新RowSet物件中特定行的列值。

獲取所需的列並使用相應的getXXX()方法更新所需的列,例如,如果您需要在第一列插入一個整數值,在第二列插入一個字串值,您可以使用updateInt()和updateString()方法:

rowSet.updateInt(1, integerValue);
rowSet.updateString(2, "stringValue");

修改/更新列值後,使用**updateRow()**方法將這些更改更新到RowSet。

要使這些更改反映在資料庫中,請使用acceptChanges()方法接受更改。為此方法,您需要傳入用於與資料庫建立連線的連線物件。

示例

假設我們在資料庫中有一個名為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            |
+----+-------------+--------------+--------------+--------------+-------+----------------+

下面的JDBC程式將鍵盤的價格降低了5000。

import java.sql.Connection;
import java.sql.DriverManager;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class UpdatingCatchedRowSet {
   public static void main(String args[]) throws Exception {
      String mysqlUrl = "jdbc:mysql:///sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      con.setAutoCommit(false);
      //Creating the RowSet object
      RowSetFactory factory = RowSetProvider.newFactory();
      CachedRowSet rowSet = factory.createCachedRowSet();
      //Setting the query/command
      rowSet.setCommand("select * from ProductSales");
      //Executing the command
      rowSet.execute(con);
      System.out.println("Contents of the table: ");
      while(rowSet.next()) {
         if(rowSet.getString("ProductName").equals("Key-Board")) {
            rowSet.updateInt("Price", rowSet.getInt("Price")-5000 );
            rowSet.updateRow();
         }
      }
      rowSet.acceptChanges();
      rowSet.beforeFirst();
      System.out.println("Contents of the table after updating the row");
      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("");
      }
   }
}

輸出

Connection established......
Connection established......
Contents of the table:
Contents of the table after updating the row
ID: 1, Product Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 05:30:00Price: 2000, 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

更新於:2019年7月30日

546 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.