Example of a simple SQLJ application

A simple SQLJ application demonstrates the basic elements that JDBC applications need to include.

Figure 1. Simple SQLJ application
import sqlj.runtime.*;                                              1 
import java.sql.*;

#sql context EzSqljCtx;                                             3a 
#sql iterator EzSqljNameIter (String LASTNAME);                     4a 

public class EzSqlj {
 public static void main(String args[]) 
    throws SQLException
 {
    EzSqljCtx ctx = null;
    String URLprefix = "jdbc:db2:";
    String url;
    url = new String(URLprefix + args[0]);
                                            // Location name is an input parameter
    String hvmgr="000010";                                          2 
    String hvdeptno="A00";
    try {                                                           3b 
      Class.forName("com.ibm.db2.jcc.DB2Driver");
    } catch (Exception e)
    {
       throw new SQLException("Error in EzSqlj: Could not load the driver");
    }
    try
    {
       System.out.println("About to connect using url: " + url);
       Connection con0 = DriverManager.getConnection(url);          3c 
                                            // Create a JDBC Connection
       con0.setAutoCommit(false);           // set autocommit OFF
       ctx = new EzSqljCtx(con0);                                   3d 

       try
       {
         EzSqljNameIter iter;
         int count=0;

         #sql [ctx] iter = 
           {SELECT LASTNAME FROM EMPLOYEE};                         4b 
                                            // Create result table of the SELECT
         while (iter.next()) {                                      4c 
            System.out.println(iter.LASTNAME()); 
                                            // Retrieve rows from result table
            count++;
         }
         System.out.println("Retrieved " + count + " rows of data");
         iter.close();                      // Close the iterator
       }
       catch( SQLException e )                                       5 
       {
          System.out.println ("**** SELECT SQLException...");
          while(e!=null) {
            System.out.println ("Error msg: " + e.getMessage());
            System.out.println ("SQLSTATE: " + e.getSQLState());
            System.out.println ("Error code: " + e.getErrorCode());
            e = e.getNextException(); // Check for chained exceptions
          }
       }
       catch( Exception e )
       {
          System.out.println("**** NON-SQL exception   = " + e);
          e.printStackTrace();
       }
       try
       {
         #sql [ctx]                                                  4d 
           {UPDATE DEPARTMENT SET MGRNO=:hvmgr 
              WHERE DEPTNO=:hvdeptno};   // Update data for one department                                  
                                                                     6 
         #sql [ctx] {COMMIT};            // Commit the update
       }
       catch( SQLException e )
       {
          System.out.println ("**** UPDATE SQLException...");
          System.out.println ("Error msg: " + e.getMessage() + ". SQLSTATE=" +
            e.getSQLState() + " Error code=" + e.getErrorCode());
          e.printStackTrace();
       }
       catch( Exception e )
       {
          System.out.println("**** NON-SQL exception   = " + e);
          e.printStackTrace();
       }
       ctx.close();                                                  7  
  }
  catch(SQLException e)
  {
     System.out.println ("**** SQLException ...");
     System.out.println ("Error msg: " + e.getMessage() + ". SQLSTATE=" + 
       e.getSQLState() + " Error code=" + e.getErrorCode());
     e.printStackTrace();
  }
  catch(Exception e)
  {
    System.out.println ("**** NON-SQL exception = " + e);
    e.printStackTrace();
  }
 }
}

Notes to Figure 1:

Note Description
1 These statements import the java.sql package, which contains the JDBC core API, and the sqlj.runtime package, which contains the SQLJ API. For information on other packages or classes that you might need to access, see "Java packages for SQLJ support".
2 String variables hvmgr and hvdeptno are host identifiers, which are equivalent to host variables. See "Variables in SQLJ applications" for more information.
3a, 3b, 3c, and 3d These statements demonstrate how to connect to a data source using one of the three available techniques. See "Connecting to a data source using SQLJ" for more details.

Step 3b (loading the JDBC driver) is not necessary if you use JDBC 4.0 or later.

4a , 4b, 4c, and 4d These statements demonstrate how to execute SQL statements in SQLJ. Statement 4a demonstrates the SQLJ equivalent of declaring an SQL cursor. Statements 4b and 4c show one way of doing the SQLJ equivalent of executing an SQL OPEN CURSOR and SQL FETCHes. Statement 4d shows how to do the SQLJ equivalent of performing an SQL UPDATE. For more information, see "SQL statements in an SQLJ application".
5 This try/catch block demonstrates the use of the SQLException class for SQL error handling. For more information on handling SQL errors, see "Handling SQL errors in an SQLJ application". For more information on handling SQL warnings, see "Handling SQL warnings in an SQLJ application".
6 This is an example of a comment. For rules on including comments in SQLJ programs, see "Comments in an SQLJ application".
7 This statement closes the connection to the data source. See "Closing the connection to the data source in an SQLJ application".