![]() | |
|
The javax.sql.RowSet
is the interface that adds support to the JDBC API for the
JavaBeans component model. A rowset, which can be used as a JavaBeans component in a visual Bean
development environment, can be created and configured at design time and executed at run time.
The RowSet
interface provides a set of JavaBeans properties that allow a
RowSet
instance to be configured to connect to a JDBC data source and read some data
from the data source. A group of setter methods (setInt(...)
, setBytes(...)
,
setString(...)
, and so on) provide a way to pass input parameters to a rowset's command
property. This command is the SQL query the rowset uses when it gets its data from a relational
database, which is generally the case.
The RowSet
interface supports JavaBeans events, allowing other components in an
application to be notified when an event occurs on a rowset, such as a change in its value.
The RowSet
interface is unique in that it is intended to be implemented using the
rest of the JDBC API. In other words, a RowSet
implementation is a layer of software
that executes "on top" of a JDBC driver. Implementations of the RowSet
interface can
be provided by anyone, including JDBC driver vendors who want to provide a RowSet
implementation as part of their JDBC products.
A RowSet
object may make a connection with a data source and maintain that connection
throughout its life cycle, in which case it is called a connected rowset.
A rowset may also make a connection with a data source, get data from it, and then close the connection.
Such a rowset is called a disconnected rowset. A disconnected rowset
may make changes to its data while it is disconnected and then send the changes back to the original
source of the data, but it must reestablish a connection to do so.
A disconnected rowset may have a reader (a RowSetReader
object) and a writer (a
RowSetWriter
object) associated with it. The reader may be implemented in many
different ways to populate a rowset with data, including getting data from a non-relational data
source. The writer can also be implemented in many different ways to propagate changes made to
the rowset's data back to the underlying data source.
Rowsets are easy to use. The RowSet
interface extends the standard
java.sql.ResultSet
interface. The RowSetMetaData
interface extends the
java.sql.ResultSetMetaData
interface.
The java.sql.ResultSet
interface requires a persistent connection with a database to
invoke the INSERT, UPDATE, and DELETE row operations on the database table data. The RowSet
interface extends the ResultSet
interface and is a container for tabular data that
may operate without being connected to the data source. Thus, the RowSet
interface
reduces the overhead of a persistent connection with the database.
In Java 5 SE, five new implementations of RowSet
— JdbcRowSet
,
CachedRowSet
, WebRowSet
, FilteredRowSet
, and
JoinRowSet
— were introduced. The WebRowSet
interface extends the
RowSet
interface and is the XML document representation of a RowSet
object. A WebRowSet
object represents a set of fetched database table rows, which
may be modified without being connected to the database.
You can use an instance of RowSetFactory
to create a RowSet
object. The
following example uses an instance of RowSetFactory
to create the
JdbcRowSet
object, jdbcRs
:
public void testJdbcRowSet(String username, String password) throws SQLException { RowSetFactory myRowSetFactory = null; JdbcRowSet jdbcRs = null; ResultSet rs = null; Statement stmt = null; try { myRowSetFactory = RowSetProvider.newFactory(); jdbcRs = myRowSetFactory.createJdbcRowSet(); jdbcRs.setUrl("jdbc:myDriver:myAttribute"); jdbcRs.setUsername(username); jdbcRs.setPassword(password); jdbcRs.setCommand("SELECT COF_NAME, SUP_ID, PRICE, SALES, TOTAL FROM COFFEES"); jdbcRs.execute(); // ...
The following statement creates the RowSetProvider
object myRowSetFactory
with the default RowSetFactory
implementation, com.sun.rowset.RowSetFactoryImpl
:
RowSetFactory myRowSetFactory = RowSetProvider.newFactory(); CachedRowSet crs = myRowSetFactory.createCachedRowSet()
Alternatively, if your JDBC driver has its own RowSetFactory
implementation, you can
specify it as an argument of newFactory(...)
method:
RowSetFactory rsf = RowSetProvider.newFactory("com.sun.rowset.RowSetFactoryImpl", null); WebRowSet wrs = rsf.createWebRowSet();
The following statements create the JdbcRowSet
object jdbcRs
and configure
its database connection properties:
jdbcRs = myRowSetFactory.createJdbcRowSet(); jdbcRs.setUrl("jdbc:myDriver:myAttribute"); jdbcRs.setUsername(username); jdbcRs.setPassword(password);
The RowSetFactory
interface contains methods to create the different types of
RowSet
implementations available in JDBC 4.1 and later:
package javax.sql.rowset; public interface RowSetFactory { CachedRowSet createCachedRowSet(); // Creates a new instance of a CachedRowSet FilteredRowSet createFilteredRowSet(); // Creates a new instance of a FilteredRowSet JdbcRowSet createJdbcRowSet(); // Creates a new instance of a JdbcRowSet JoinRowSet createJoinRowSet(); // Creates a new instance of a JoinRowSet WebRowSet createWebRowSet(); // Creates a new instance of a WebRowSet }
javax.sql.rowset.JdbcRowSet
interface.
A wrapper around a ResultSet
object that makes it possible to use the result set as a
JavaBeans component. Thus, a JdbcRowSet
object can be one of the Beans that a tool makes available
for composing an application. Because a JdbcRowSet
is a connected rowset, that is, it continually
maintains its connection to a database using a JDBC technology-enabled driver, it also effectively makes the
driver a JavaBeans component.
Because it is always connected to its database, an instance of JdbcRowSet
can simply take calls invoked
on it and in turn call them on its ResultSet
object. As a consequence, a result set can, for example, be
a component in a Swing application.
Another advantage of a JdbcRowSet
object is that it can be used to make a ResultSet
object
scrollable and updatable. All RowSet
objects are by default scrollable and updatable. If the driver and database being
used do not support scrolling and/or updating of result sets, an application can populate a JdbcRowSet
object with the data of a ResultSet
object and then operate on the JdbcRowSet
object as
if it were the ResultSet
object.
javax.sql.rowset.CachedRowSet
interface.
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 supports getting data only from a
ResultSet
object, but developers can extend the SyncProvider
implementations to provide access to
other tabular data sources.
An application can modify the data in a CachedRowSet
object, and those modifications can then be propagated back
to the source of the data:
public interface CachedRowSet extends RowSet, Joinable { ... void acceptChanges(); // Propagates row update, insert and delete changes made // to this CachedRowSet object to the underlying data source. void acceptChanges(Connection con); // Propagates all row update, insert and delete // changes to the data source backing this CachedRowSet object using the // specified Connection object to establish a connection to the data source. ... }
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).
javax.sql.rowset.WebRowSet
interface.
The WebRowSetImpl
provides the standard reference implementation, which may be extended if required.
The standard WebRowSet XML Schema definition describes the standard XML document format required when describing a RowSet
object in XML and must be used be all standard implementations of the WebRowSet
interface to ensure interoperability.
In addition, the WebRowSet
schema uses specific SQL/XML Schema annotations, thus ensuring greater cross platform
inter-operability.
javax.sql.rowset.JoinRowSet
interface.
The JoinRowSet
interface provides a mechanism for combining related data from different RowSet
objects into
one JoinRowSet
object, which represents an SQL JOIN
. In other words, a JoinRowSet
object acts as
a container for the data from RowSet
objects that form an SQL JOIN
relationship.
The Joinable
interface provides the methods for setting, retrieving, and unsetting a match column, the basis for establishing
an SQL JOIN
relationship. The match column may alternatively be set by supplying it to the appropriate version of the
JointRowSet
method addRowSet
.
public interface JoinRowSet extends WebRowSet { ... void addRowSet(Joinable rowset); // Adds the given RowSet object to this JoinRowSet object. void addRowSet(RowSet[] rowset, int[] columnIdx); // Adds one or more RowSet objects contained // in the given array of RowSet objects to this // JoinRowSet object and sets the match column for each of the RowSet objects to the match columns in the given array of column indexes. void addRowSet(RowSet[] rowset, String[] columnName); // Adds one or more RowSet objects contained // in the given array of RowSet objects to this // JoinRowSet object and sets the match column for // each of the RowSet objects to the match columns in the given array of column names. void addRowSet(RowSet rowset, int columnIdx); // Adds the given RowSet object to this JoinRowSet object and // sets the designated column as the match column for the RowSet // object. void addRowSet(RowSet rowset, String columnName); // Adds rowset to this JoinRowSet object and sets the // designated column as the match column. ... }
The type of JOIN
to be established is determined by setting one of the JoinRowSet
constants using the method
setJoinType
. The following SQL JOIN
types can be set:
CROSS_JOIN
FULL_JOIN
INNER_JOIN
- the default if no JOIN type has been set
LEFT_OUTER_JOIN
RIGHT_OUTER_JOIN
Note that if no type is set, the JOIN will automatically be an inner join.
javax.sql.rowset.FilteredRowSet
interface.
There are occasions when a RowSet
object has a need to provide a degree of filtering to its contents. One
possible solution is to provide a query language for all standard RowSet
implementations; however, this is
an impractical approach for lightweight components such as disconnected RowSet
objects. The
FilteredRowSet
interface seeks to address this need without supplying a heavyweight query language
along with the processing that such a query language would require.
A JDBC FilteredRowSet
standard implementation implements the RowSet
interfaces and extends the
CachedRowSet
class. The CachedRowSet
class provides a set of protected cursor manipulation
methods, which a FilteredRowSet
implementation can override to supply filtering support.
By implementing a javax.sql.rowset.Predicate
interface (see the Range
class below), a FilteredRowSet
could then be used as described below:
public class Range implements Predicate { public boolean evaluate(RowSet rs) { ... } ... }
FilteredRowSet frs = new FilteredRowSetImpl(); frs.populate(rs); Range name = new Range("Alpha", "Bravo", "columnName"); frs.setFilter(name); frs.next() // only names from "Alpha" to "Bravo" will be returned
In the example above, we initialize a Range
object which implements the javax.sql.rowset.Predicate
interface. This object expresses the following constraints: All rows outputted or modified from this FilteredRowSet
object must fall between the values 'Alpha
' and 'Bravo
' both values inclusive, in the column
'columnName
'. If a filter is applied to a FilteredRowSet
object that contains no data that falls
within the range of the filter, no rows are returned.
This framework allows multiple classes implementing predicates to be used in combination to achieved the required filtering result with out the need for query language processing.
![]() ![]() ![]() |