IBM DB2 Manual page 140

Table of Contents

Advertisement

Using a named iterator in an SQLJ application
Use a named iterator to refer to each of the columns in a result table by name.
The steps in using a named iterator are:
1. Declare the iterator.
124
Application Programming Guide and Reference for Java
You declare any result set iterator using an iterator declaration clause. This causes
an iterator class to be created that has the same name as the iterator. For a
named iterator, the iterator declaration clause specifies the following
information:
v The name of the iterator
v A list of column names and Java data types
v Information for a Java class declaration, such as whether the iterator is
public or static
v A set of attributes, such as whether the iterator is holdable, or whether its
columns can be updated
When you declare a named iterator for a query, you specify names for each of
the iterator columns. Those names must match the names of columns in the
result table for the query. An iterator column name and a result table column
name that differ only in case are considered to be matching names. The named
iterator class that results from the iterator declaration clause contains accessor
methods. There is one accessor method for each column of the iterator. Each
accessor method name is the same as the corresponding iterator column name.
You use the accessor methods to retrieve data from columns of the result table.
You need to specify Java data types in the iterators that closely match the
corresponding DB2 column data types. See "Java, JDBC, and SQL data types"
for a list of the best mappings between Java data types and DB2 data types.
You can declare an iterator in a number of ways. However, because a Java class
underlies each iterator, you need to ensure that when you declare an iterator,
the underlying class obeys Java rules. For example, iterators that contain a
with-clause must be declared as public. Therefore, if an iterator needs to be
public, it can be declared only where a public class is allowed. The following
list describes some alternative methods of declaring an iterator:
v As public, in a source file by itself
This method lets you use the iterator declaration in other code modules, and
provides an iterator that works for all SQLJ applications. In addition, there
are no concerns about having other top-level classes or public classes in the
same source file.
v As a top-level class in a source file that contains other top-level class
definitions
Java allows only one public, top-level class in a code module. Therefore, if
you need to declare the iterator as public, such as when the iterator includes
a with-clause, no other classes in the code module can be declared as public.
v As a nested static class within another class
Using this alternative lets you combine the iterator declaration with other
class declarations in the same source file, declare the iterator and other
classes as public, and make the iterator class visible to other code modules or
packages. However, when you reference the iterator from outside the nesting
class, you must fully-qualify the iterator name with the name of the nesting
class.
v As an inner class within another class

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents