SQL/MX Programming Manual for Java
SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java—523726-003
3-54
Positioned Iterator Versus Named Iterator
Using the next() Method
Use the next() method from the generated iterator class NamIter to determine the
success of retrieving a row from an iterator result set. While next() returns true, the
next row of the result set is retrieved and the accessor methods for retrieving the
column values are invoked. The next() method returns false when the iterator fails to
retrieve the next row.
Note that the end-of-data handling of iterators differs from testing for the end-of-data
condition in embedded SQL programs in C or COBOL. When you reach the end of
data in a cursor (or result set), SQLSTATE returns 02000. In an embedded SQL
program in C or COBOL, you must code if (SQLSTATE = "02000") after each
fetch to determine if you have read a row or not. For more information, see the
SQL/MX Programming Manual for C and COBOL. In an SQLJ program, you check for
the end-of-data condition in a named iterator by coding itername.next().
Using the close() Method
Use the close() method from the generated iterator class NamIter to close an
iterator after using it. Because iterators hold database resources, you should explicitly
close an iterator that you are no longer using rather than wait for garbage collection to
free resources.
Positioned Iterator Versus Named Iterator
Choosing either a positioned iterator or a named iterator is basically a stylistic decision.
Positioned iterators are more similar to a cursor in an embedded SQL program in C or
COBOL. Named iterators are more similar to JDBC result sets.
A positioned iterator might be preferable because:
•
The code of the positioned iterator is more compact and potentially easier to read
and maintain than that of a named iterator.
•
Named iterators will fail if the column name of the underlying table changes.
A named iterator might be worth the extra coding because:
•
Named iterators enable the application logic to decide when and which columns to
fetch.
•
By using the accessor methods of a named iterator, you do not need to assign the
column values to host variables but instead can use the accessor methods in
places where host variables might be used. For example, you can pass
iter.Salary() as an argument to a method that converts the annual salary to a
biweekly pay rate.