SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-53
Named Iterator
Retrieving Rows From a Named Iterator
Within a loop, retrieve rows from a named iterator by using the next() method and
the accessor methods included in the named iterator class. This step is similar to
reading a cursor in an embedded SQL program in C or COBOL.
Using Accessor Methods
Within a named iterator class, SQLJ creates an accessor method for each column
name in the iterator declaration clause. The accessor method name is the same name
as the column name in the iterator declaration clause and is case-sensitive. Use an
accessor method to retrieve data from the corresponding column of the result set.
By using accessor methods, you can individually assign iterator column values to Java
variables. In a Java assignment statement, the Java data type of the variable must be
compatible with the SQL data type that is returned by the corresponding accessor
method. The individual assignment of iterator column values to Java variables enables
you to use logic for each Java assignment statement within the next() loop.
Example
This example retrieves data from a named iterator by using accessor methods in a
next() loop:
// Declare named iterator class NamIter, which has accessor
// methods Last_Name() and Salary()
#sql public iterator NamIter
(String Last_Name, BigDecimal Salary);
// Declare an iterator variable of NamIter class
NamIter 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 salary, last_name
FROM samdbcat.persnl.employee};
// Retrieve rows and assign columns while next() returns true
while (iter.next()) {
empname = iter.Last_Name();
salary = iter.Salary();
System.out.println(empname + " earns " + salary);
}
iter.close();