The following example SQLJ application, App.sqlj, uses static SQL to retrieve and update data from the EMPLOYEE table of the DB2® sample database.
import java.sql.*; import sqlj.runtime.*; import sqlj.runtime.ref.*; #sql iterator App_Cursor1 (String empno, String firstnme) ; // 1 #sql iterator App_Cursor2 (String) ; class App { /********************** ** Register Driver ** **********************/ static { try { Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance(); } catch (Exception e) { e.printStackTrace(); } } /******************** ** Main ** ********************/ public static void main(String argv[]) { try { App_Cursor1 cursor1; App_Cursor2 cursor2; String str1 = null; String str2 = null; long count1; // URL is jdbc:db2:dbname String url = "jdbc:db2:sample"; DefaultContext ctx = DefaultContext.getDefaultContext(); if (ctx == null) { try { // connect with default id/password Connection con = DriverManager.getConnection(url); con.setAutoCommit(false); ctx = new DefaultContext(con); } catch (SQLException e) { System.out.println("Error: could not get a default context"); System.err.println(e) ; System.exit(1); } DefaultContext.setDefaultContext(ctx); } // retrieve data from the database System.out.println("Retrieve some data from the database."); #sql cursor1 = {SELECT empno, firstnme FROM employee}; // 2 // display the result set // cursor1.next() returns false when there are no more rows System.out.println("Received results:"); while (cursor1.next()) // 3 { str1 = cursor1.empno(); // 4 str2 = cursor1.firstnme(); System.out.print (" empno= " + str1); System.out.print (" firstname= " + str2); System.out.println(""); } cursor1.close(); // 9 // retrieve number of employee from the database #sql { SELECT count(*) into :count1 FROM employee }; // 5 if (1 == count1) System.out.println ("There is 1 row in employee table"); else System.out.println ("There are " + count1 + " rows in employee table"); // update the database System.out.println("Update the database."); #sql { UPDATE employee SET firstnme = 'SHILI' WHERE empno = '000010' }; // retrieve the updated data from the database System.out.println("Retrieve the updated data from the database."); str1 = "000010"; #sql cursor2 = {SELECT firstnme FROM employee WHERE empno = :str1}; // 6 // display the result set // cursor2.next() returns false when there are no more rows System.out.println("Received results:"); while (true) { #sql { FETCH :cursor2 INTO :str2 }; // 7 if (cursor2.endFetch()) break; // 8 System.out.print (" empno= " + str1); System.out.print (" firstname= " + str2); System.out.println(""); } cursor2.close(); // 9 // rollback the update System.out.println("Rollback the update."); #sql { ROLLBACK work }; System.out.println("Rollback done."); } catch( Exception e ) { e.printStackTrace(); } } }
2Initialize the iterator. The iterator object cursor1 is initialized using the result of a query. The query stores the result in cursor1.
3Advance the iterator to the next row. The cursor1.next() method returns a Boolean false if there are no more rows to retrieve.
4Move the data. The named accessor method empno() returns the value of the column named empno on the current row. The named accessor method firstnme() returns the value of the column named firstnme on the current row.
5SELECT data into a host variable. The SELECT statement passes the number of rows in the table into the host variable count1.
6 Initialize the iterator. The iterator object cursor2 is initialized using the result of a query. The query stores the result in cursor2.
7Retrieve the data. The FETCH statement returns the current value of the first column declared in the ByPos cursor from the result table into the host variable str2.
8Check the success of a FETCH.INTO statement. The endFetch() method returns a Boolean true if the iterator is not positioned on a row, that is, if the last attempt to fetch a row failed. The endFetch() method returns false if the last attempt to fetch a row was successful. DB2 attempts to fetch a row when the next() method is called. A FETCH...INTO statement implicitly calls the next() method.
9Close the iterators. The close() method releases any resources held by the iterators. You should explicitly close iterators to ensure that system resources are released in a timely fashion.
For background information on this example, see Embed SQL Statements in your Java™ application.