SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-49
Positioned Iterator
SQLJ PositionedIterator interface and inherits its method signatures. See
sqlj.runtime.PositionedIterator Interface on page B-6.
Declaring an Iterator Variable
After coding the iterator declaration clause, declare an iterator variable by using the
iterator class definition. Place this declaration among other Java variable declarations
within the class of the SQLJ source file. For example, declare the iterator iter by
using the PosIter class definition:
PosIter iter;
Assigning Query Results to a Positioned Iterator Object
Instantiate an iterator object by assigning the result set of a query to an iterator
variable. This step is similar to opening a cursor in an embedded SQL program in C or
COBOL. For example, use an assignment clause to bind the result set of a SELECT
statement to the iterator iter:
#sql iter = { SELECT last_name, salary
FROM samdbcat.persnl.employee };
For the syntax, see the Assignment Clause on page A-5.
The query can be as complex as you desire and can include GROUP BY, HAVING,
ORDER BY, UNION, DISTINCT, and so on. However, the order, number, and SQL data
types of the columns in the select list must correspond to the Java data types listed in
the iterator declaration clause. In the previous example, the LAST_NAME
(CHARACTER data type) and SALARY (NUMERIC data type) columns in the select list
correspond to the String and BigDecimal types in the iterator declaration clause.
Retrieving Rows From a Positioned Iterator
Within a loop, retrieve rows from a positioned iterator and assign the retrieved data to
Java host variables by using a FETCH statement. This step is similar to reading a
cursor in an embedded SQL program in C or COBOL.
Using the FETCH Statement
The FETCH statement puts data from each row of the result set into host variables
specified in the INTO clause. The order of the host variables in the INTO clause must
correspond to the result set columns.
Examples
This example shows a positioned iterator in a do-while loop:
// Declare positioned iterator class PosIter
#sql public iterator PosIter (String, BigDecimal);
// Declare an iterator variable of PosIter class