Is RowSet Scrollable? Explain with an example?


A RowSet object is similar to ResultSet, it also stores tabular data, in addition to the features of a ResultSet. A RowSet follows the JavaBeans component model.

If you Retrieve a ResultSet object by default the cursor of it moves only in forward direction. i.e. you can retrieve the contents of it from first to last.

But, in a scrollable result set, the cursor can scroll forward and backward and you can retrieve data backward too.

To make ResultSet object scrollable, you need to create one as shown below −

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

Whereas, a RowSet object is Scrollable by default. Therefore, whenever the underlying database does not provide a Scrollable ResultSet object you can use RowSet instead.

Example

Assume we have a table named Dispatches in the database with 5 records as shown below −

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

Following JDBC program Retrieves the contents of a RowSet from last to first

import java.sql.DriverManager;
import javax.sql.RowSet;
import javax.sql.rowset.RowSetProvider;
public class ScrolableUpdatableRowSet {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Creating the RowSet object
      RowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
      //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 Dispatches");
      rowSet.setCommand("SELECT ProductName, CustomerName, Price, Location from Dispatches where price > ?");
      rowSet.setInt(1, 2000);
      rowSet.execute();
      rowSet.afterLast();
      while(rowSet.previous()) {
         System.out.print("Product Name: "+rowSet.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", ");
         System.out.print("Price: "+rowSet.getString("Price")+", ");
         System.out.print("Location: "+rowSet.getString("Location"));
         System.out.println("");
      }
   }
}

Output

Product Name: Headset, Customer Name: Jalaja, Price: 6000, Location: Vijayawada
Product Name: Mobile, Customer Name: Vanaja, Price: 9000, Location: Vijayawada
Product Name: Mouse, Customer Name: Puja, Price: 3000, Location: Vijayawada
Product Name: Key-Board, Customer Name: Raja, Price: 7000, Location: Hyderabad

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

320 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements