What Is a CachedRowSet? in JDBC

CachedRowSet (javax.sql.CachedRowSet) object is a disconnected (from the database) ResultSet object. CachedRowSet is a JavaBean, and this means that like any other JavaBean, implementations of javax.sql.CachedRowSet may be serialized. This allows for CachedRowSet objects to be serialized, sent to remote clients, updated, and then sent back to the server (including the database server). For example, a CachedRowSet object can be used as the data model for a Java servlet or JavaServer Pages (JSP) entry or edit form. The following is taken from the J2SE 5.0 documentation:

A CachedRowSet object is a container for rows of data that caches its rows in memory, which makes it possible to operate without always being connected to its data source. Further, it is a JavaBeans component and is scrollable, updatable, and serializable.

A CachedRowSet object typically contains rows from a result set, but it can also contain rows from any file with a tabular format, such as a spread sheet. The reference implementation (com.sun.rowset.CachedRowSetImpl class) supports getting data only from
a ResultSet object, but developers can extend the SyncProvider implementations to provide
access to other tabular data sources.

A CachedRowSet object can be used as the data model for a Java server-side entry or edit form. An application can modify the data in a CachedRowSet object, and those modifications can then be propagated back to the source of the data. According to J2SE 5.0 documentation, a CachedRowSet object is

. . . a disconnected rowset, which means that it makes use of a connection to its data source only briefly. It connects to its data source while it is reading data to populate itself with rows and again while it is propagating changes back to its underlying data source. The rest of the time, a CachedRowSet object is disconnected, including while its data is being modified. Being disconnected makes a RowSet object much leaner and therefore much easier to pass to another component. For example, a disconnected RowSet object can be serialized and passed over the wire to a thin client such as a personal digital assistant (PDA).

Therefore, a CachedRowSet object is special in that it can operate without being connected to its data source—that is, it is a disconnected RowSet object. It gets the name “CachedRowSet” from the fact that it stores (caches) its data in memory so that it can operate on its own data rather than on the data stored in a database.

CachedRowSet is defined as
package javax.sql.rowset;
import javax.sql.RowSet;
import javax.sql.rowset.Joinable;
public interface CachedRowSet
extends RowSet, Joinable {


To create a CachedRowSet object, use its JavaBeans properties. The CachedRowSet properties allow it to connect to a database and retrieve data on its own. Table describes some of the properties necessary to initialize a CachedRowSet without a preexisting database connection.

The following code fragment illustrates creating a CachedRowSet object, setting its properties, and executing the command string in its command property:

import java.sql.Connection; // database connection object
import java.sql.ResultSet; // result set object
import java.sql.Statement; // statement object
import java.sql.rowset.CachedRowSet; // JdbcRowSet definition
import com.sun.rowset.CachedRowSetImpl; // Sun's reference impl. of CachedRowSet
import jcb.util.DatabaseUtil; // a utility class to close resources

String query = "select author, title from books";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
CachedRowSet crs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
// create CachedRowSet and populate
crs = new CachedRowSetImpl();
crs.populate(rs); // populate using a ResultSet object
// here you may use crs (a CachedRowSet object)
catch (SQLException e) {
// handle exception

finally {
// note that the connection is being closed
// here you can use CachedRowSet without being connected to database:
if (crs != null) {
while (crs.next()) {
String author = crs.getString(1);
String title = crs.getString(2);

At this point, crs contains all of the selected data in the books table. After creating a CachedRowSet object, you may use ResultSet methods because the CachedRowSet object is effectively operating on a ResultSet object. We can navigate the rows in crs (by using the next() method), retrieve column values, update column values, insert new rows, and so on. For example, the next three lines of code go to the third row and retrieve the value in the first and second columns using ResultSet methods. You might be wondering whether this allows going back to any row, and the answer is yes. You also might be wondering what happens if any updates are done to this CachedRowSet object. If you do not commit changes to your database, then changes will not be reflected on the database at all.

String author = jrs.getString(1);
String title = jrs.getString(2);

Once you create a CachedRowSet object, then you can extract its metadata by using this

RowSetMetaData rsMetaData = (RowSetMetaData) crs.getMetaData();
If (rsMetaData == null) {
// vendor has not implemented the RowSetMetaData interface
else {
// you can invoke RowSetMetaData methods to get JdbcRowSet's metadata
int numberOfColumns = rsMetaData.getColumnCount();

Sun’s reference implementation class, com.sun.rowset.CachedRowSetImpl, which is
defined next, has two constructors.
CachedRowSetImpl Class Definition
package com.sun.rowset;
public class CachedRowSetImpl
extends javax.sql.rowset.BaseRowSet
implements javax.sql.RowSet,
java.lang.Cloneable {


CachedRowSetImpl Class Constructors

  • CachedRowSetImpl(): Constructs a new default CachedRowSetImpl object with the capacity to hold 100 rows
  • CachedRowSetImpl(java.util.Hashtable env): Provides a CachedRowSetImpl instance

with the same default properties as the zero parameter constructor

You can make changes to a CachedRowSet object (i.e., its data) and then commit the changes back into the database. Therefore, for a given CachedRowSet object, you can not only read its data, but you can also propagate changes back to the database. For example, the following code segment updates a CachedRowSet object, and then commits changes back into its data source:

crs.updateString(1, "Donald E. Knuth");
where the commitToDatabase() method is defined as
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet;
import jcb.util.DatabaseUtil; // for closing resources

public static boolean commitToDatabase(CachedRowSet crs) {
if (crs == null) {
// there is nothing to commit
return true;
Connection conn = null;
try {
conn = getConnection();
// propagate changes and close connection
return true;
catch (SQLException se) {
// handle exception
return false;
catch (Exception e) {
// handle exception
return false;
finally {

To use a CachedRowSet object with JavaServer Pages (JSP), see the article “Get Disconnected with CachedRowSet.

The Oracle database imposes some constraints on CachedRowSet objects: all the constraints that apply to an updatable result set are applicable here except serialization, since OracleCachedRowSet is serializable. The SQL query has the following constraints:

  • It references only a single table in the database.
  • It contains no join operations.
  • It selects the primary key of the table it references.

In addition, a SQL query should also satisfy these conditions if inserts are to be performed:

  • It selects all of the non-nullable columns in the underlying table.
  • It selects all columns that do not have a default value.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics