Java Reference
In-Depth Information
24.7 RowSet Interface
In the preceding examples, you learned how to query a database by explicitly establishing
a Connection to the database, preparing a Statement for querying the database and exe-
cuting the query. In this section, we demonstrate the RowSet interface , which configures
the database connection and prepares query statements automatically. The interface Row-
Set provides several set methods that allow you to specify the properties needed to establish
a connection (such as the database URL, username and password of the database) and cre-
ate a Statement (such as a query). RowSet also provides several get methods that return
these properties.
Connected and Disconnected RowSet s
There are two types of RowSet objects—connected and disconnected. A connected RowSet
object connects to the database once and remains connected while the object is in use. A
disconnected RowSet object connects to the database, executes a query to retrieve the data
from the database and then closes the connection. A program may change the data in a
disconnected RowSet while it's disconnected. Modified data can be updated in the data-
base after a disconnected RowSet reestablishes the connection with the database.
Package javax.sql.rowset contains two subinterfaces of RowSet JdbcRowSet and
CachedRowSet . JdbcRowSet , a connected RowSet , acts as a wrapper around a ResultSet
object and allows you to scroll through and update the rows in the ResultSet . Recall that
by default, a ResultSet object is nonscrollable and read only—you must explicitly set the
result-set type constant to TYPE_SCROLL_INSENSITIVE and set the result-set concurrency
constant to CONCUR_UPDATABLE to make a ResultSet object scrollable and updatable. A
JdbcRowSet object is scrollable and updatable by default. CachedRowSet , a disconnected
RowSet , caches the data of a ResultSet in memory and disconnects from the database.
Like JdbcRowSet , a CachedRowSet object is scrollable and updatable by default. A Cached-
RowSet object is also serializable , so it can be passed between Java applications through a
network, such as the Internet. However, CachedRowSet has a limitation—the amount of
data that can be stored in memory is limited. Package javax.sql.rowset contains three
other subinterfaces of RowSet .
Portability Tip 24.5
A RowSet can provide scrolling capability for drivers that do not support scrollable Re-
sultSet s.
Using a RowSet
Figure 24.29 reimplements the example of Fig. 24.23 using a RowSet . Rather than estab-
lish the connection and create a Statement explicitly, Fig. 24.29 uses a JdbcRowSet object
to create a Connection and a Statement automatically.
Class RowSetProvider (package javax.sql.rowset ) provides static method new-
Factory which returns a an object that implements interface RowSetFactory (package
javax.sql.rowset ) that can be used to create various types of RowSet s. Lines 18-19 in
the try -with-resources statement use RowSetFactory method createJdbcRowSet to
obtain a JdbcRowSet object.
Lines 22-24 set the RowSet properties that the DriverManager uses to establish a data-
base connection. Line 22 invokes JdbcRowSet method setUrl to specify the database
 
 
Search WWH ::




Custom Search