Databases Reference
In-Depth Information
5.12.7 Exception handling
Exception handling can be done in the same way we handle exceptions in a
JDBC program.
5.12.8 JDBC and SQLj
JDBC and SQLj can be used together in a single application. A connection object
in JDBC is similar to the ConnectionContext in SQLj. A connection object can be
retrieved from a ConnectionContext object and vice versa. Getting a
ConnectionContext object from a Connection object is shown in Example 5-27 on
page 240. A connection object from ConnectionContext can be retrieved using
the method getConnection of ConnectionContext object.
In the same way, an iterator in SQLj and the JDBC ResultSet can be retrieved
from each other. To get an iterator from the ResultSet object, use the following
command:
#sql iterator={CAST :result-set }
Before doing the cast, make sure that iterator definition is the same as the result
set definition. For the named iterator, column name and the data types of the
columns defined in the iterator should be the same as those of the result set. For
a positioned iterator, the number of columns and data type should match those of
the result set. Apart from that, properties such as scrollability, updatability, and
holdability should match the definition of the iterator. For details about setting
these properties for SQLj iterator, refer to “Updatable and scrollable iterators” on
page 242.
A ResultSet object can be retrieved from the iterator by calling the getResultSet
method of the iterator class.
Example 5-36 shows how to get an iterator from the ResultSet object.
Example 5-36 Creating an iterator from the ResultSet object
#sql public iterator positionIterator (int, String);
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection con=DriverManager.getConnection(url);
con.setAutoCommit(false);
ctx ctx1=new ctx(con);
positionIterator iterator;
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select poid, status from
purchaseorder");
Search WWH ::




Custom Search