Describe use of iterators in SQLj projects.
What will be an ideal response?
When a SQL query returns more than one row in a Java program, SQLj iterator is used.
An iterator is similar to JDBC result set, but columns are given data types in iterator. An
iterator is based on cursor in the SQL query. SQLj constructs an iterator class for iterator
declaration. The instance variables and methods from iterator class are available in the
program. There are two types iterator declarations:
? Named Iterator – data types and column names are specified.
? Positional Iterator – only data types are specified.
Named Iterator: The following statement declares a named iterator DeptIter.
#sql iterator DeptIter(int i, String n, String l);
Iterator contains three columns with their data type. SQLj creates a class with the same
name, DeptIter. Then, a null reference named di to DeptIter class is declared, which is
populated with a SELECT query. It is important to use column aliases for columns
retrieved, and column aliases must be same as column names in the iterator.
DeptIter di = null;
#sql di={SELECT deptid i,deptname n,location l FROM dept};
Positional Iterator: Positional iterator is similar to named iterator, but it is declared with
data types that positionally match data types of columns retrieved with the SELECT
query. For example, the positional iterator is declared only with data as follows:
#sql iterator DeptIter(int, String, String);
The iterator is instantiated and populated with a SQL query as given in the following
statements:
DeptIter di = null;
#sql di = {SELECT deptid, deptname, location FROM dept};
You might also like to view...
Which of the following is an example of an instant messaging service?
A) Monster B) Yahoo Messenger C) Flickr D) LinkedIn
The name that represents a cell or a range of cells should ________
A) be distinctive B) define the purpose of the selected cell or range C) be used with the cell or range reference for identification purposes D) be easy to remember