Java Reference
In-Depth Information
Chapter 18:
Using Rowsets to Display Data
In This Chapter
RowSets
add significant new capabilities to JDBC by adding JavaBeans support to the JDBC API.
Rowsets
make it easy to send tabular data over a network. They can also be used as a wrapper,
providing scrollable
ResultSets
or updatable
ResultSets
when the underlying JDBC driver does not
support them.
This chapter discusses
RowSets
, comparing them with the
ResultSets
of the JDBC core API and
illustrating the features of the different types of
RowSets
.
Understanding RowSets
A
RowSet
is an object that contains a set of rows from a
ResultSet
or some other source of tabular
data, like a file or spreadsheet. The
RowSet
object is an extension of
ResultSet
, with the added
benefit of incorporating JavaBeans support. The
RowSet
object is supported by the
RowSetMetaData
interface, which extends the
ResultSetMetaData
interface.
A
RowSet
differs significantly from a
ResultSet
in that it provides a set of JavaBeans properties to
connect to a JDBC data source and to read data from the data source for making connections,
executing commands, and reading and writing data to and from the data source. These properties
include the following:
rowSet.setUrl( url );
rowSet.setUsername( login );
rowSet.setPassword( password );
rowSet.getConnection();
rowSet.setCommand("SELECT * FROM sysusers");
rowSet.execute();
Since
RowSets
are JavaBeans, notice that they follow the JavaBeans model for setting and getting
properties such as the Username and Password. They also follow the JavaBeans API to handle events
such as changes in a column value. Being JavaBeans,
RowSets
use the Java event model to notify
listeners when the
RowSet
is changed.
Rowsets
make it easy to send tabular data over a network. They can also be used as a wrapper,
providing scrollable
ResultSets
or updatable
ResultSets
when the underlying JDBC driver does not
support them.
There are two main types of
RowSets
— connected and disconnected. A connected
RowSet
, like a
ResultSet
, maintains a connection to a data source for as long as the
RowSet
is in use. A
disconnected
RowSet
gets a connection to a data source to load data or to propagate changes back to
the data source, but most of the time it does not have a connection open.
While it is disconnected, a
RowSet
does not need a JDBC driver or the full JDBC API, so its footprint is
very small. Since it is not continually connected to its data source, a disconnected
RowSet
stores its
data in memory. It also maintains metadata about the columns it contains and information about its
internal state.
Creating and Using a RowSet
The simplest way to explain how a
RowSet
works is to use an example.
Listing 18-1
illustrates the use
of a
JdbcRowSet
to retrieve some names and e-mail addresses from the Contacts Table created as
part of the LEDES database in
Chapter 2
.