Using scrollable iterators in an SQLJ application
In addition to moving forward, one row at a time, through a result table, you might want to move backward or go directly to a specific row. The IBM® Data Server Driver for JDBC and SQLJ provides this capability.
About this task
An iterator in which you can move forward, backward, or to a specific row is called a scrollable iterator. A scrollable iterator in SQLJ is equivalent to the result table of a database cursor that is declared as SCROLL.
Like a scrollable cursor, a scrollable iterator can be insensitive or sensitive. A sensitive scrollable iterator can be static or dynamic. Insensitive means that changes to the underlying table after the iterator is opened are not visible to the iterator. Insensitive iterators are read-only. Sensitive means that changes that the iterator or other processes make to the underlying table are visible to the iterator. Asensitive means that if the cursor is a read-only cursor, it behaves as an insensitive cursor. If it is not a read-only cursor, it behaves as a sensitive cursor.
If a scrollable iterator is static, the size of the result table and the order of the rows in the result table do not change after the iterator is opened. This means that you cannot insert into result tables, and if you delete a row of a result table, a delete hole occurs. If you update a row of the result table so that the row no longer qualifies for the result table, an update hole occurs. Fetching from a hole results in an SQLException.
If a scrollable iterator is dynamic, the size of the result table and the order of the rows in the result table can change after the iterator is opened. Rows that are inserted or deleted with INSERT and DELETE statements that are executed by the same application process are immediately visible. Rows that are inserted or deleted with INSERT and DELETE statements that are executed by other application processes are visible after the changes are committed.
Procedure
To create and use a scrollable iterator, you need to follow these steps:
Example
#sql context Ctx; // Create connection context class Ctx
#sql iterator ScrollIter implements sqlj.runtime.Scrollable 1
(String EmpNo, String LastName);
{
…
Ctx ctxt =
new Ctx("jdbc:db2://sysmvs1.stl.ibm.com:5021/NEWYORK",
userid,password,false); // Create connection context object ctxt
// for the connection to NEWYORK
ScrollIter scrliter; 2
#sql [ctxt]
scrliter={SELECT EMPNO, LASTNAME FROM EMPLOYEE};
scrliter.afterLast();
while (scrliter.previous()) 4a
{
System.out.println(scrliter.EmpNo() + " " 4c
+ scrliter.LastName());
}
scrliter.close(); 5
}