How to insert data into a CachedRowSet in JDBC? Explain?


The CachedRowSet is the base implementation of disconnected row sets. It connects to the data source, reads data from it, disconnects with the data source and the processes the retrieved data, reconnects to the data source and writes the modifications.

Creating a CachedRowSet

You can create a Cached RowSet object using the createCachedRowSet() method of the RowSetFactory.

You can create a RowSetFactory object using the newfactory() method of the RowSetProvider method.

Create a CachedRowSet object using the above mentioned methods as shown below −

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

Connecting to the data source

After creating a RowSet object you need to connect it to the required DataSource.

You can connect to a DataSource by setting values to the properties such as username, password, url and, data source name as −

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

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

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

Preparing and executing the command statement

A cached row set has a command property to which you can pass the queries. Set the required query to this property using the setCommand() method.

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

You can execute the query that the RowSet object holds using the execute() method.

rowSet.execute();

Inserting a row in a CachedRowSet

  • The moveToInsertRow() method of the CachedRowSet interface navigates the cursor to the position where you need to insert the next record.

    Therefore, move the cursor to the appropriate position to insert a row using this method.

  • The updateXXX() methods of the CachedRowSet interface allows you to insert/update values in to the RowSet object.

    Add values to the new row using these methods for example if you need to insert an integer value at 1st column and, String value at 2nd column you can do so using the updateInt() and updateString() methods as −

rowSet.updateInt(1, integerValue);
rowSet.updateString(2, "stringValue");
  • The insertRow() method inserts the row to the CachedRowSet and to the table as well.

    Therefore, insert the above created row in to CachedRowSet object and the table using this method.

Example

Assume we have a table named ProductSales in the database with the following content −

+----+-------------+--------------+--------------+--------------+-------+----------------+
| 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             |
+----+-------------+--------------+--------------+--------------+-------+----------------+

Following example Retrieves the contents of the above mentioned table in to a CachedRowSet object and, inserts a new record in to it.

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://localhost/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("");
      }
   }
}

Output

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
raja
Published on 04-Apr-2019 09:30:16
Advertisements