SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-48
Iterators and Result Sets
Iterators and Result Sets
In an SQLJ program, iterators enable you to access multiple rows of data in the result
set of a query. An iterator is a Java object that serves the same purpose as a cursor in
an embedded program in C or COBOL and as a result set in JDBC. Unlike a cursor,
you can pass an iterator as a parameter to a method.
Iterators are either positioned or named. You cannot use both types of iterators for a
single result set.
For more information, see:
Positioned Iterator on page 3-48
Named Iterator on page 3-51
Positioned Iterator Versus Named Iterator on page 3-54
Positioned UPDATE and DELETE Statements on page 3-55
Passing Iterators as Parameters on page 3-56
Converting Between SQLJ Iterators and JDBC Result Sets on page 3-56
Positioned Iterator
For a positioned iterator, the types of the columns of the iterator declaration correspond
in order to the types of the columns of the result set. That is, the columns of the iterator
declaration are bound to the columns of the result set by position.
Declaring a Positioned Iterator Class
Declare an iterator as positioned by using an iterator declaration clause, which
specifies a list of Java data types corresponding to columns in the result set. For the
syntax, see the Iterator Declaration Clause on page A-2.
The number of Java data types in the iterator declaration must be equal to the number
of columns in the result set. The Java data types in the iterator declaration must be
compatible with the positionally corresponding SQL data types of the columns in the
result set. Specifically, the data types of the SQL columns in the result set must be
convertible to the Java data types of the positionally corresponding iterator columns.
For more information, see Optimal Data Type Mappings on page 3-37.
Place the iterator declaration clause where an SQLJ declaration clause is allowed in an
SQLJ program. The iterator declaration clauses usually appear at the beginning (or the
top-level scope) of an SQLJ source file. The placement of the iterator declaration
clause affects the visibility of the iterator class for other classes. For more information,
see Placement of Declaration Clauses on page 3-2.
In this example, an iterator declaration clause names a positioned iterator class
PosIter:
#sql public iterator PosIter (String, BigDecimal);
The SQLJ translator replaces each iterator declaration clause with a Java definition of
the iterator class in the Java source (.java) file. The iterator class implements the