Chapter 4. Sqlj Application Programming; Example Of A Simple Sqlj Application - IBM DB2 Manual

Table of Contents

Advertisement

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.
Figure 27. Simple SQLJ application
import sqlj.runtime.*;
import java.sql.*;
#sql context EzSqljCtx;
#sql iterator EzSqljNameIter (String LASTNAME);
public class EzSqlj {
© Copyright IBM Corp. 1998, 2008
public static void main(String args[])
throws SQLException
{
EzSqljCtx ctx = null;
String URLprefix = "jdbc:db2:";
String url;
url = new String(URLprefix + args[0]);
String hvmgr="000010";
String hvdeptno="A00";
try {
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);
con0.setAutoCommit(false);
ctx = new EzSqljCtx(con0);
try
{
// Location name is an input parameter
// Create a JDBC Connection
// set autocommit OFF
1
3a
4a
2
3b
3c
3d
101

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents