6-6 Oracle Fusion Middleware Programming JDBC for Oracle WebLogic Server
6.4.4 Importing Classes and Interfaces for a CachedRowSet
For standard RowSets, you must import the following classes: javax.sql.rowset.CachedRowSet;
weblogic.jdbc.rowset.RowSetFactory;
6.4.5 Creating a CachedRowSet
Rowsets are created from a factory interface. To create a rowset with WebLogic Server, follow these main steps:
1.
Create a RowSetFactory instance, which serves as a factory to create rowset objects for use in your application. You can specify database connection properties in the
RowSetFactory so that you can create RowSets with the same database connectivity using fewer lines of code.
RowSetFactory rsfact = RowSetFactory.newInstance;
2.
Create a WLCachedRowSet and cast it as a javax.sql.rowset.CachedRowSet object. By default, the WebLogic newCachedRowSet RowSetFactory method
creates a WLCachedRowSet object. You can use it as-is, but if you prefer to use the standard CachedRowSet object, you can cast the object as such.
CachedRowSet rs = rsfact.newCachedRowSet;
6.4.6 Setting CachedRowSet Properties
There are numerous rowset properties, such as concurrency type, data source name, transaction isolation level, and so forth, that you can set to determine the behavior of
the rowset. You are required to set only those properties that are needed for your particular use of the rowset. For information about available properties, see the
Javadoc for the javax.sql.rowset.BaseRowSet class at
http:java.sun.comjavase6docsapijavaxsqlrowsetBaseRowSe t.html
.
6.4.7 Database Connection Options
In most applications, you populate a rowset with data from a database. You can set rowset database connectivity in any of the following ways:
■
Automatically with a data source—You can use the setDataSourceName method to specify the JNDI name of a JDBC data source. When you call
execute and acceptChanges, the rowset gets a database connection from
the data source, uses it, and returns it to the pool of connections in the data source. This is a preferred method.
rs.setDataSourceNameexamples-dataSource-demoPool;
■
Manually get a database connection—In your application, you can get a database connection before the rowset needs it, and then pass the connection object as a
parameter in the execute and acceptChanges methods. You must also close the connection as necessary.
Lookup DataSource and get a connection ctx = new InitialContextht;
javax.sql.DataSource ds = javax.sql.DataSource ctx.lookup myDS; conn = ds.getConnection;
Pass the connection to the rowset
Using RowSets with WebLogic Server 6-7
rs.executeconn; For more information about JDBC data sources, see
Section 2.1, Getting a Database Connection from a DataSource Object.
■
Load the JDBC driver for a direct connection—When you load the JDBC driver and set the appropriate properties, the rowset creates a database connection when
you call execute and acceptChanges. The rowset closes the connection immediately after it uses it. The rowset does not keep the connection between the
execute
and acceptChanges method calls. Class.forNameorg.apache.derby.jdbc.ClientDriver;
rs.setUrljdbc:derby:localhost:1527demo; rs.setUsernameexamples;
rs.setPasswordexamples; rs.execute;
■
Set connectivity properties in the RowSetFactory—When you set database connection properties in the RowSetFactory, all rowsets created from the
RowSetFactory inherit the connectivity properties. The preferred method is to lookup a data source and then set the DataSource property in the RowSetFactory
with the setDataSource method.
Lookup DataSource and get a connection ctx = new InitialContextht;
javax.sql.DataSource ds = javax.sql.DataSource ctx.lookup myDS;
Set the datasource property on the RowSetFactory rsfact.setDataSourceds;
6.4.8 Populating a CachedRowSet