SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-57
Converting Between SQLJ Iterators and JDBC
Result Sets
Example
Suppose that you have a result set and want to cast it to an iterator:
// Declare named iterator class NamIter
#sql public iterator NamIter
(String Last_Name, BigDecimal Salary);
// Declare an iterator variable of NamIter class
NamIter iter;
// Other Java variable declarations and initializations
String empname = null;
BigDecimal salary = null;
String query = "SELECT salary, last_name"
+ " FROM samdbcat.persnl.employee"
+ " WHERE deptnum = ?";
// Prepare query
PreparedStatement ps = prepareStatement(query);
// Set parameter from command-line input
ps.setInt(1, args[0]);
// Execute query
ResultSet rs = ps.executeQuery();
// Cast JDBC result set as iterator
#sql iter = {CAST :rs};
// Retrieve rows and assign columns while next() returns true
while (iter.next()) {
empname = iter.Last_Name();
salary = iter.Salary();
System.out.println(empname + " earns " + salary);
}
iter.close();
Creating a JDBC Result Set From an Iterator
You can apply the getResultSet() method to an iterator to return a JDBC result set.
See getResultSet() on page B-8.
Example
Suppose that you have an iterator and want to get the corresponding result set:
// Declare named iterator class NamIter
#sql public iterator NamIter
(String Last_Name,BigDecimal Salary);
// Declare an iterator variable of NamIter class
NamIter iter;