SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-50
Positioned Iterator
PosIter iter;
// Other Java variable declarations and initializations
String empname = null;
BigDecimal salary = null;
// Assign the result set of a query to an iterator object
// named iter
#sql iter = {SELECT last_name, salary
FROM samdbcat.persnl.employee};
// Retrieve rows while endFetch() returns false
do {
empname = null;
#sql {FETCH :iter INTO :empname, :salary};
if (empname != null) && (salary != null)
System.out.println(empname + " earns " + salary);
}
while (!iter.endFetch());
iter.close();
This example shows a positioned iterator in a while loop instead of a do-while
loop:
...
// Retrieve the first row from the result set
#sql {FETCH :iter INTO :empname, :salary};
// Retrieve remaining rows while endFetch() returns false
while (!iter.endFetch()) {
System.out.println(empname + " earns " + salary);
#sql {FETCH :iter INTO :empname, :salary};
}
iter.close();
This example shows a positioned iterator in a while loop:
...
// Retrieve rows while endFetch() returns false
while (true) {
#sql {FETCH :iter INTO :empname, :salary};
if (iter.endfetch()) break;
System.out.println(empname + " earns " + salary);
}
iter.close();
Using the endFetch() Method
Use the endFetch() method from the generated iterator class PosIter to determine
the success of fetching a row from an iterator result set. The endFetch() method is
initially false and continues to return false if the last attempt to fetch a row from the
result set succeeds. While endFetch() returns false, the FETCH statement retrieves