A RowSet is a wrapper around a ResultSet object. It can be connected, disconnected from the database, and can be serialized. It maintains a JavaBean component by setting the properties. You can pass a RowSet object over the network. By default, the RowSet object is scrollable and updatable.
This diagram will give you more idea about ResultSet and RowSet class hierarchy. It is also useful to find out which RowSet implementation is Connected and which one is Disconnected. You can see that there are two major implementations of the RowSet interface, JdbcRowSet which is connected, and CachedRowSet which is disconnected.
RowSet object is of two types as listed below s follows:
- Connected Row Sets
- Disconnected Row Sets
Let us discuss both of the above types as follows:
Type 1: Connected Row Sets
A connected RowSet object connects to the database using a JDBC driver. It establishes a connection with the database and, carries out the required operations. The connection is maintained until the RowSet object is closed.
Type 2: Disconnected Row Sets
A disconnected RowSet object connects to the database only while reading from the database and while writing to it. A disconnected RowSet object does not hold a connection with the database/data source while processing the data and thus it operates independently.
A disconnected RowSet object is almost the same as a connected RowSet object except they are:
- Lighter in weight compared to connected RowSet objects.
- Serializable.
- Able to send data to lightweight clients such as mobiles etc.
JDBC provides four classes that represent disconnected RowSet objects that ae listed below as follows:
- CachedRowSet
- WebRowSet
- JoinRowSet
- FilteredRowSet
Let us discuss each of the above classes in detail to perceive their functioning as follows:
Class 1: CachedRowSet
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 processes the retrieved data, reconnects to the data source, and writes the modifications.
Class 2: WebRowSet:
A WebRowSet extends the CachedRowSet.
Class 3: JoinRowSet
This is able to send data to lightweight clients such as mobiles etc.
Class 4: FilteredRowSet:
This enables you to cut down the number of rows that are visible in a RowSet.
Now getting to the eccentric concept in order to figure out the difference between Connected and Disconnected RowSet
As we already have mentioned above the main difference between connected and disconnected RowSet is that the former always keeps a database connection, while the latter doesn’t. It connects to the database, gets the data, and then closes the connection. Here are a couple of more differences between them:
1) Disconnected databases are connected to the database only when they want to read or write, all the times they are disconnected from the database, on the other hand, Connected databases keep JDBC connections alive all time.
2) Only JdbcRowSet from JDBC 4.1 API is connected, rest like CachedRowSet, WebRowSet are disconnected RowSet implementation.
3) Disconnected RowSet is Serializable and that’s why suitable for sending over the network
This table provides a nice comparison of features supported by different RowSet implementations in the JDBC API.
Features | JdbcRowSet | CacheRowSet | WebRowSet |
---|---|---|---|
Scrollable | Not allowed | Not allowed | Not allowed |
Updatable | Not allowed | Not allowed | Not allowed |
Connected | Not allowed | Not allowed | Not allowed |
Disconnected | Not allowed | Not allowed | |
Serializable | Not allowed | Not allowed | |
Generate XML | Not allowed | ||
Consume XML | Not allowed |
RowSet in Java
Implementation: We will retrieve employees’ records using RowSet instead of the ResultSet interface. So far, you might have only seen a ResultSet object returning query result, but from JDBC 4.1 API, you can use any implementation of RowSet implementation for querying the database and retrieving results.
- In the below JDBC example, we will use the JdbcRowSet class. In order to create an object of JdbcRowSet, you need a RowSetFactory, which can be created by using newFactory() method of RowSetProvider.
- Once you have an object of RowSet, you can pass configuration details like database URL, username, and password by calling their respective setProperties() method like setURL(), setUsername(), and setPassword().
- After configuration, it’s time to specify our SQL SELECT Query, we will use a method called setCommand() for providing the query. Now you can execute the query by calling execute() method. Did you notice a much cleaner and simple API?
- After query execution, it’s time to retrieve data, which is very similar to what you are used to by calling various getXXX() methods from ResultSet. You just need to iterate through RowSet by checking hasNext() method in the while loop and getting data row by row.
Example
Java
// Java Program to illustrate how to use RowSet, // RowSetProvider, and RowSetFactory in JDBC // Importing required classes import java.sql.*; import javax.sql.rowset.*; // Main class // RowSetDemo public class GFG { // Main driver method public static void main(String[] args) { // Setting username and password to access database String url String userName = "root" ; String password = "mysql123" ; // Try block to check for exceptions try { // Step 1: Create a factory object for rowset RowSetFactory rowSetFactory = RowSetProvider.newFactory(); // Step 2: Creating a JDBC rowset object from // the factory JdbcRowSet rowSet = rowSetFactory.createJdbcRowSet(); // Step 3: Setting connection properties rowSet.setUrl(url); rowSet.setUsername(userName); rowSet.setPassword(password); // Step 4: Setting SQL Query to execute rowSet.setCommand( "SELECT * FROM contact" ); // Step 5: Executing an SQL query rowSet.execute(); // Print and display the attributes System.out.println( "id \tName \tDepartment \tEmail \tSalary" ); // Iterating over RowSet object // using next() method while (rowSet.next()) { // Print and display all employee // attributes System.out.println( rowSet.getInt( "id" ) + "\t" + rowSet.getString( "name" ) + "\t" + rowSet.getString( "department" ) + "\t" + rowSet.getString( "email" ) + "\t" + rowSet.getString( "salary" )); } } // Catch block to handle the exceptions catch (SQLException sqle) { // Display the exception along with line number // using printStackTrace() method sqle.printStackTrace(); } } } |
Output:
Id Name Department Email Salary 1 Jack Sales jack@bluechip.com 9000 2 Jill Marketing jill@bluechip.com 8000 3 Tom Accounts tom@bluechip.com 7000
It is clear from the above output that RowSet is used to retrieve data from the database instead of ResultSet.