Named iterators

The name of each Java™ variable of a named iterator must match the name of a column returned by your SELECT statement; order is irrelevant. The matching of SQL column names and iterator column names is case insensitive.

For example, the following statement generates a named iterator class called CustRec:
#sql iterator CustRec(
int    customer_num, 
String fname, 
String lname ,
String company ,
String address1 ,
String address2 ,
String city ,
String state ,
String zipcode ,
String phone 
);

This iterator can hold the result set of any query that returns the columns defined in the iterator class. You use accessor methods of the same name as each iterator column to obtain the returned data, as shown in the example in A simple embedded SQLJ program. The SQLJ translator uses the iterator column names to create accessor methods. Iterator column names are case sensitive; therefore, you must use the correct case when you specify an accessor method.

You cannot use the FETCH...INTO statement with named iterators.

The following example illustrates the use of named iterators:
// Declare Iterator of type CustRec
CustRec cust_rec;

#sql cust_rec = { SELECT *  FROM customer };

int row_cnt = 0;
while ( cust_rec.next() )
{
System.out.println("===================================");
System.out.println("CUSTOMER NUMBER :" + cust_rec.customer_num());
System.out.println("FIRST NAME      :" + cust_rec.fname());
System.out.println("LAST NAME       :" + cust_rec.lname());
System.out.println("COMPANY         :" + cust_rec.company());
System.out.println("ADDRESS         :" + cust_rec.address1() +"\n" +
"                 " + cust_rec.address2());
System.out.println("CITY            :" + cust_rec.city());
System.out.println("STATE           :" + cust_rec.state());
System.out.println("ZIPCODE         :" + cust_rec.zipcode());
System.out.println("PHONE           :" + cust_rec.phone());
System.out.println("===================================");
System.out.println("\n\n");
row_cnt++;
}
System.out.println("Total No Of rows Selected :" + row_cnt);
cust_rec.close() ;

The next() method of the iterator object advances processing to successive rows of the result set. It returns FALSE after it fails to find a row to retrieve.

The Java compiler detects type mismatches for the accessor methods.

The validity of the types and names of the iterator columns and their related columns in the SELECT statement are checked at translation time if you perform online checking. For information about setting up online checking, see Online checking.