Chapter 4. SQLJ application programming
Writing a SQLJ application has much in common with writing an SQL application
in any other language.
In general, you need to do the following things:
v Import the Java packages that contain SQLJ and JDBC methods.
v Declare variables for sending data to or retrieving data from DB2 tables.
v Connect to a data source.
v Execute SQL statements.
v Handle SQL errors and warnings.
v Disconnect from the data source.
Although the tasks that you need to perform are similar to those in other
languages, the way that you execute those tasks, and the order in which you
execute those tasks, is somewhat different.
Example of a simple SQLJ application
A simple SQLJ application demonstrates the basic elements that JDBC applications
need to include.
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
{
Figure 27. Simple SQLJ application
© Copyright IBM Corp. 1998, 2008 101