Tuesday, November 6, 2007

Rowsets in JAVA

A javax.sql.RowSet object encapsulates a set of rows that have been retrieved
from a tabular data source. Because the RowSet interface includes an event
notification mechanism and supports getting and setting properties, every RowSet
object is a JavaBeansTM component. This means, for example, that a rowset can be
used as a JavaBeans component in a visual JavaBeans development environment. As
a result, a RowSet instance can be created and configured at design time, and its
methods can be executed at run time.

Rowsets at Design Time

Properties

The RowSet interface provides a set of JavaBeans properties that allow a RowSet
instance to be configured to connect to a data source and retrieve a set of rows.
CODE EXAMPLE

Setting properties for a RowSet object

sets some properties for the RowSet object rset.
rset.setDataSourceName("jdbc/SomeDataSourceName");
rset.setTransactionIsolation(
Connection.TRANSACTION_READ_COMMITTED);
rset.setCommand("SELECT NAME, BREED, AGE FROM CANINE");


The data source name property is used by a RowSet object to look up a
DataSource object in a JNDI naming service. (Relational databases are the most
common type of data source used by rowsets.) The DataSource object is used to
create a connection to the physical data source that it represents. The transaction
isolation property specifies that only data that was produced by committed
transactions may be read by the rowset. Lastly, the command property specifies the
command that will be executed to retrieve a set of rows. In this case, the NAME,
BREED, and AGE columns for all rows in the CANINE table are retrieved.

Events

RowSet components support JavaBeans events, which allows other JavaBeans
components in an application to be notified when an event on a rowset occurs. A
component that wishes to register for RowSet events must implement the
RowSetListener interface. Event listeners are registered with a rowset by calling
the addRowSetListener method as shown below. Any number of listeners may be
registered with an individual RowSet object.

Adding a listener to a RowSet object

adds one listener to
the RowSet object rset.
RowSetListener listener = ...;
rset.addRowSetListener(listener);


Rowsets can generate three different types of events:

1. Cursor movement events — indicate that the rowset’s cursor has moved
2. Row change events — indicate that a particular row has been inserted, updated or
deleted
3. Rowset change events — indicate that the entire contents of a rowset have
changed, which may happen, for example, when the method RowSet.execute
is called.

When an event occurs, the appropriate listener method is called behind the scenes to
notify the registered listener(s). If a listener is not interested in a particular kind of
event, it may implement the method for that event so that it does nothing. Listener
methods take a RowSetEvent object, which identifies the RowSet object that is the
source of the event.

Rowsets at Run Time

Parameters
The command property in CODE EXAMPLE 18-1 was set with a simple SQL command
that takes no input parameters, but it could also have been set with a command that
accepts input parameters. The RowSet interface provides a group of setter methods
for setting these input parameters.

CODE EXAMPLE
shows a command that takes a String input parameter. The
RowSet.setString method is used to pass the input parameter value to the
RowSet object rset. Typically, the command property is specified at design time,
whereas parameters are not set until run time when their values are known.

rset.setCommand("SELECT NAME, BREED, AGE FROM CANINE WHERE NAME = ?");
rset.setString(1, "spot");

CODE EXAMPLE

Setting parameters in a RowSet object’s command

Command Execution


A rowset may be filled with data by calling the RowSet.execute method. This
method uses the appropriate property values internally to connect to a data source
and retrieve some data. The RowSet interface includes the properties that are
needed to connect to a data source. The exact properties that must be set may vary
between RowSet implementations, so developers need to check the documentation
for the particular rowset they are using. The method execute throws an
SQLException if the necessary properties have not been set. The current contents of
a rowset, if any, are lost when the method execute is called.

Traversing a Rowset

The javax.sql.RowSet interface extends the java.sql.ResultSet interface, so
in many ways a rowset behaves just like a result set. In fact, most components that
make use of a RowSet component will likely treat it as a ResultSet object. A
RowSet object is simply a ResultSet object that can function as a JavaBeans

component. The code below shows how to iterate forward through a rowset. Notice
that since a rowset is a result set, this code is identical to the code that would be
used to iterate forward through a result set.

// iterate forward through the rowset

rset.beforeFirst();
while (rset.next()) {
System.out.println(rset.getString(1) + " " + rset.getFloat(2));
}

Printing all the rows in a two-column RowSet object
Other cursor movements, such as iterating backward through the rowset and
positioning the cursor on a specific row, are also done the same way they are for
result sets.

No comments: