EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #141 background imageLoading...
Page #141 background image
When you declare an iterator in this way, you can instantiate it only within
an instance of the nesting class. However, you can declare the iterator and
other classes in the file as public.
You cannot cast a JDBC ResultSet to an iterator if the iterator is declared as
an inner class. This restriction does not apply to an iterator that is declared
as a static nested class. See Use SQLJ and JDBC in the same application for
more information on casting a ResultSet to a iterator.
2. Create an instance of the iterator class.
You declare an object of the named iterator class to retrieve rows from a result
table.
3. Assign the result table of a SELECT to an instance of the iterator.
To assign the result table of a SELECT to an iterator, you use an SQLJ
assignment clause. The format of the assignment clause for a named iterator is:
#sql context-clause iterator-object={select-statement};
See SQLJ assignment-clause and SQLJ context-clause for more information.
4. Retrieve rows.
Do this by invoking accessor methods in a loop. Accessor methods have the
same names as the corresponding columns in the iterator, and have no
parameters. An accessor method returns the value from the corresponding
column of the current row in the result table. Use the NamedIterator.next()
method to move the cursor forward through the result table.
To test whether you have retrieved all rows, check the value that is returned
when you invoke the next method. next returns a boolean with a value of
false if there is no next row.
5. Close the iterator.
Use the NamedIterator.close method to do this.
The following code demonstrates how to declare and use a named iterator. The
numbers to the right of selected statements correspond to the previously-described
steps.
Related concepts
“SQL statement execution in SQLJ applications” on page 112
“Data retrieval in SQLJ applications” on page 123
Related tasks
#sql iterator ByName(String LastName, Date HireDate); 1
// Declare named iterator ByName
{
ByName nameiter; // Declare object of ByName class 2
#sql [ctxt]
nameiter={SELECT LASTNAME, HIREDATE FROM EMPLOYEE}; 3
// Assign the result table of the SELECT
// to iterator object nameiter
while (nameiter.next()) // Move the iterator through the result 4
// table and test whether all rows retrieved
{
System.out.println( nameiter.LastName() + " was hired on "
+ nameiter.HireDate()); // Use accessor methods LastName and
// HireDate to retrieve column values
}
nameiter.close(); // Close the iterator 5
}
Figure 39. Example of using a named iterator
Chapter 4. SQLJ application programming 125

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability