Using RowSets

RowSets are part of the javax.sql package. They extend java.sql.ResultSet and add support for JavaBeans to enable developers to use RowSets in visual JavaBean development editors. A company can therefore define several data views as JavaBeans, and developers can manipulate the data views in a visual way. RowSets contain a number of properties that can be set at design time, such as a SQL query. Other parameters can be set at runtime, when the RowSet is actually used within a program. RowSets support JavaBean type of events. Using such an event model allows RowSets to be notified whenever a cursor moves or a row is updated, for example.

Using RowSet methods

The following are the main methods provided in the RowSet interface. Only the methods that a developer will typically use are listed.

RowSet’s Methods

void addRowSetListener(RowSetListener listener) void clearParameters() void execute() void removeRowSetListener(RowSetListener listener) void setCommand(java.lang.String cmd) void setConcurrency(int concurrency) void setDataSourceName(java.lang.String name) void setEscapeProcessing(boolean enable) void setMaxFieldSize(int max) void setMaxRows(int max) void setPassword(java.lang.String password) void setQueryTimeout(int seconds) void setReadOnly(boolean value) void setTransactionIsolation(int level) void setType(int type) void setTypeMap(java.util.Map map) void setUsername(java.lang.String name) void setXXX(int parameterIndex, ...)

When creating a RowSet, you use the void setCommand(java.lang.String cmd) method to set the SQL text associated to the RowSet. This is typically a query that returns rows of data. You can pass parameters to SQL queries to the RowSet object at runtime, in the same way parameters are set for callable statements and prepared statements. (A question mark indicates the place of a parameter.) The void setXXX(int parameterIndex, ...) methods are used to pass values to these parameters, where XXX can be one of String, Int, Long, Blob, Clob, Object, and so on.

addRowSetListener(RowSetListener listener) and removeRowSetListener (RowSetListener listener) are used to register and detach an object that implements the RowSetListener interface.

void setDataSourceName(java.lang.String name) is used to set the JNDI name of a data source that the RowSet must use to connect to the database, and void setUsername(java.lang.String name) and void setPassword(java.lang. String password) are used to pass login and password information. Finally, void execute() causes the SQL statement to be sent to the database and processed. The returned rows are then available using the standard getXXX() methods of the ResultSet interface.

Designing and using RowSets

Designing and using a RowSet is very simple. The example in Listing 8-4 illustrates the different steps. Note that because the JDBC API doesn’t provide an implementation of RowSets, you must refer to the documentation of particular JDBC drivers, for example, for details on how to instantiate the rowset.

You can set the initial parameters at the design time of a rowset. In this example, the SQL query contains one parameter that will be set at runtime. After executing the rowset, individual rows can be fetched one by one, just as rows are accessed in a usual result set. Note that all ResultSet methods are applicable to the RowSet object, as RowSet inherits from ResultSet.

Listing 8-4: Using RowSets

start example
... // setting initial parameters rowset.setDataSourceName("jdbc/MyDataSource"); rowset.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); rowset.setUserName("dba"); rowset.setPassword("hotjava"); rowset.setMaxRows(1000); rowset.setCommand("SELECT emp_no, firstname, name, dept WHERE location = ?"); ... // setting runtime parameters rowset.setString(1, "MOUNTAIN VIEW"); ... // executing rowset rowset.execute(); ... // accessing the rowset content rowset.beforeFirst(); while ( {         System.out.println("Employee Number: " +                    rowset.getInt(1));         System.out.println("Firstname: " +                    rowset.getString(2));         System.out.println("Name: " +                    rowset.getString(3));         System.out.println("Department: " +                    rowset.getString(4)); } rowset.close();
end example

JDBC 3. 0. JAVA Database Connectivity
JDBC 3: Java Database Connectivity
ISBN: 0764548751
EAN: 2147483647
Year: 2002
Pages: 148 © 2008-2017.
If you may any questions please contact us: