SQL/MX Programming Manual for Java
Sample Programs
HP NonStop SQL/MX Programming Manual for Java—523726-003
C-11
Translating the Program
Translating the Program
At an OSS prompt, enter this command to translate the SQLJ source file by running
the SQLJ translator:
java sqlj.tools.Sqlj -createMDF SampleDML.sqlj
/* Declare a positioned iterator class named PosIter.
* The iterator declaration clause may not appear within
* the method body. */
#sql iterator PosIter (String, String, int);
/* The doIterator() method executes a positioned iterator */
void doIterator () throws SQLException {
// Declare an iterator named iter as positioned iterator
// named PosIter
PosIter iter;
// Declare host variables
String firstName = null;
String lastName = null;
int empNum = 0;
/* Set the default catalog and schema for the database
* objects of this method. */
#sql [ctx] {DECLARE CATALOG 'SAMDBCAT'};
#sql [ctx] {DECLARE SCHEMA 'PERSNL'};
// Bind the results of the query to the iterator
#sql [ctx] iter = {SELECT first_name, last_name, empnum
FROM employee
WHERE (deptnum = 1000)
AND (jobcode = 100)};
// Fetch the results of the iterator into host variables
while (true) { // Keep looping until endFetch is true
#sql {FETCH :iter INTO :firstName, :lastName, :empNum};
if (iter.endFetch())
break; // No more rows, quit the loop
System.out.println(firstName + " " + lastName +
"has employee number " + empNum);
}
} // End of doIterator() method
} // End of SampleDML class
Example C-2. SampleDML.sqlj—Inserting, Deleting, Updating, and Selecting
Data (page 3 of 3)