Application Building Guide


SQLJ Programs

Note:To build and run SQLJ programs with the IBM Java Development Kit for UNIX, OS/2, and Windows 32-bit operating systems, you must turn off the just-in-time compiler of the JDK with the following command for your operating system:

OS/2 and Windows:
SET JAVA_COMPILER=NONE

UNIX:
export JAVA_COMPILER=NONE

The build file, bldsqlj, contains the commands to build an SQLJ applet or application. On UNIX this is a script file. On OS/2, it is the command file, bldsqlj.cmd, and on Windows, it is the batch file, bldsqlj.bat. The contents of the command and batch files are the same, and this version is presented first, followed by the UNIX script file. The applet and application building sections that follow will refer back to these build files.
Note:The SQLJ translator shipped with DB2 compiles the translated .java files into .class files. Therefore, the build files in this section do not use the java compiler.

In the following build file for OS/2 and Windows 32-bit operating systems, the first parameter, %1, specifies the name of your source file. The second parameter, %2, specifies the name of the database to which you want to connect. The third parameter, %3, specifies the user ID for the database, and %4 specifies the password. Only the first parameter, the source file name, is required. Database name, user ID, and password are optional. If no database name is supplied, the program uses the default sample database.

@echo off
rem bldsqlj -- OS/2 and Windows 32-bit operating systems
rem Builds a Java embedded SQL (SQLJ) program.
rem Usage: bldsqlj prog_name [ db_name [ userid password ]] 
 
if "%1" == "" goto error
 
rem Translate and compile the SQLJ source file
rem and bind the package to the database.
if "%2" == "" goto case1
if "%3" == "" goto case2
if "%4" == "" goto error
goto case3
:case1
   sqlj %1.sqlj
   db2profc -url=jdbc:db2:sample -prepoptions="package using %1" %1_SJProfile0 
   goto continue
:case2
   sqlj     -url=jdbc:db2:%2 %1.sqlj
   db2profc -url=jdbc:db2:%2 -prepoptions="package using %1" %1_SJProfile0
   goto continue
:case3
   sqlj     -url=jdbc:db2:%2 -user=%3 -password=%4  %1.sqlj
   db2profc -url=jdbc:db2:%2 -user=%3 -password=%4 -prepoptions="package using %1" 
     %1_SJProfile0
   goto continue
:continue
 
goto exit
 
:error
echo Usage: bldsqlj prog_name [ db_name [ userid password ]] 
 
:exit
 
@echo on

Translator and Precompile Options for bldsqlj

sqlj
The SQLJ translator (also compiles the program).

%1.sqlj
The SQLJ source file.

%1.java
The translated Java file from the SQLJ source file.

db2profc
The DB2 for Java profile customizer.

-url
Specifies a JDBC URL for establishing a database connection, such as jdbc:db2:sample.

-user
Specifies a user ID (optional parameter).

-password
Specifies a password (optional parameter).

-prepoptions
Specifies the package name for the database with the string "package using %1", where %1 is the SQLJ source file name.

%1_SJProfile0
Specifies a serialized profile for the program.

In the following UNIX script file, the first parameter, $1, specifies the name of your source file. The second parameter, $2, specifies the name of the database to which you want to connect. The third parameter, $3, specifies the user ID for the database, and $4 specifies the password. Only the first parameter, the source file name, is required. Database name, user ID, and password are optional. If no database name is supplied, the program uses the default sample database.

#! /bin/ksh
# bldsqlj script file -- UNIX platforms
# Builds a Java embedded SQL (SQLJ) sample
# Usage: bldsqlj <prog_name> [ <db_name> [ <userid> <password> ]] 
 
# Translate and compile the SQLJ source file
# and bind the package to the database.
if (($# < 2))
then
   sqlj $1.sqlj
   db2profc -url=jdbc:db2:sample -prepoptions="package using $1" $1_SJProfile0
elif (($# < 3))
then
   sqlj     -url=jdbc:db2:$2 $1.sqlj
   db2profc -url=jdbc:db2:$2 -prepoptions="package using $1" $1_SJProfile0
else
   sqlj     -url=jdbc:db2:$2 -user=$3 -password=$4  $1.sqlj
   db2profc -url=jdbc:db2:$2 -user=$3 -password=$4 -prepoptions="package using $1" 
     $1_SJProfile0
fi

Translator and Precompile Options for bldsqlj

sqlj
The SQLJ translator (also compiles the program).

$1.sqlj
The SQLJ source file.

$1.java
The translated Java file from the SQLJ source file.

db2profc
The DB2 for Java profile customizer.

-url
Specifies a JDBC URL for establishing a database connection, such as jdbc:db2:sample.

-user
Specifies a user ID (optional parameter).

-password
Specifies a password (optional parameter).

-prepoptions
Specifies the package name for the database with the string "package using $1", where $1 is the SQLJ source file name.

$1_SJProfile0
Specifies a serialized profile for the program.

Applets

Applt demonstrates an SQLJ applet that accesses a DB2 database.

To build this applet with the build file, bldsqlj, and then run it:

  1. Ensure that a web server is installed and running on your DB2 machine (server or client).
  2. Modify the Applt.html file according to the instructions in the file.
  3. Start the JDBC applet server on the TCP/IP port specified in Applt.html. For example, if in Applt.html, you specified param name=port value='6789', then you would enter:
       db2jstrt 6789
    
  4. Build the applet with this command:
       bldsqlj Applt [ <db_name> [ <userid> <password> ]]
    

    where the optional parameter <db_name> allows you to access another database instead of the default sample database. The optional parameters <userid>, and <password> are needed if the database you are accessing is on a different instance, such as if you are accessing a server from a remote client machine.

  5. Ensure that your working directory is accessible by your web browser. If it is not, copy the following files into a directory that is accessible:
       Applt.html,                             Applt.class,
       Applt_Cursor1.class,                    Applt_Cursor2.class,
       Applt_SJProfileKeys.class,              Applt_SJProfile0.ser
    
  6. Copy the files %DB2PATH%\java\db2java.zip and %DB2PATH%\java\runtime.zip on OS/2 and Windows 32-bit operating systems, or sqllib/java/db2java.zip and sqllib/java/runtime.zip on UNIX, into the same directory as your other Applt files.
  7. On your client machine, start your web browser (which must support JDK 1.1) and load Applt.html.

As an alternative to steps (1), (5) and (7), you can use the applet viewer that comes with the Java Development Kit by entering the following command in the working directory of your client machine:

   appletviewer Applt.html

You can also use the Java makefile to build this program.

Applications

App demonstrates an SQLJ application that accesses a DB2 database.

To build this application with the build file, bldsqlj, enter this command:

   bldsqlj App [ <db_name> [ <userid> <password> ]]

where the optional parameter <db_name> allows you to access another database instead of the default sample database. The optional parameters <userid>, and <password> are needed if the database you are accessing is on a different instance, such as if you are accessing a server from a remote client machine.

Run the Java interpreter on the application with this command:

   java App

You can also use the Java makefile to build this program.

Client Programs for Stored Procedures

Stclient is the client program that calls the SQLJ stored procedure class, Stserver , using the JDBC Application driver. Before building and running this client application, build the stored procedure class on the server. See "Stored Procedures".

To build this client program with the build file bldsqlj, enter this command:

   bldsqlj Stclient [ <db_name> [ <userid> <password> ]]

where the optional parameter <db_name> allows you to access another database instead of the default sample database. The optional parameters <userid>, and <password> are needed if the database you are accessing is on a different instance, such as if you are accessing a server from a remote client machine.

Run the Java interpreter on the client application with this command:

   java Stclient 

You can also use the Java makefile to build this program.

Client Programs for User-Defined Functions

UDFclie is the client program that calls the user-defined functions implemented in the server program, UDFsrv , using the JDBC application driver. Before building and running this client application, build the UDFsrv program on the server. See "User-Defined Functions (UDFs)".

To build this SQLJ client program with the build file, bldsqlj, enter this command:

   bldsqlj UDFclie [ <db_name> [ <userid> <password> ]]

where the optional parameter <db_name> allows you to access another database instead of the default sample database. The optional parameters <userid>, and <password> are needed if the database you are accessing is on a different instance, such as if you are accessing a server from a remote client machine.

Run the Java interpreter on client application with this command:

   java UDFclie

You can also use the Java makefile to build this program.

Stored Procedures

The build file, bldsqljs, contains the commands to build an SQLJ stored procedure. On UNIX this is a script file. On OS/2, it is the command file, bldsqljs.cmd, and on Windows, it is a batch file, bldsqljs.bat. The contents of the command and batch files are the same, and this version is presented first, followed by the UNIX script file.

In the following build file for OS/2 and Windows 32-bit operating systems, the first parameter, %1, specifies the name of your source file. The second parameter, %2, specifies the name of the database to which you want to connect. Since the stored procedure must be built on the same instance where the database resides, there are no parameters for user ID and password.

Only the first parameter, the source file name, is required. If no database name is supplied, the program uses the default sample database.

@echo off
rem bldsqljs -- OS/2 and Windows 32-bit operating systems
rem Builds a Java embedded SQL (SQLJ) stored procedure
rem Usage: bldsqljs prog_name [ db_name ] 
 
if "%1" == "" goto error
 
rem Translate and compile the SQLJ source file
rem and bind the package to the database.
if "%2" == "" goto case1
goto case2
:case1
   sqlj %1.sqlj 
   db2profc -url=jdbc:db2:sample -prepoptions="package using %1" %1_SJProfile0 
   goto continue
:case2
   sqlj     -url=jdbc:db2:%2 %1.sqlj 
   db2profc -url=jdbc:db2:%2 -prepoptions="package using %1" %1_SJProfile0
:continue
 
rem Copy the *.class and *.ser files to the 'function' directory.
copy %1*.class "%DB2PATH%\function"
copy %1*.ser "%DB2PATH%\function"
 
goto exit
:error
echo Usage: bldsqljs prog_name [ db_name ] 
:exit
@echo on


Translator and Precompile Options for bldsqljs

sqlj
The SQLJ translator (also compiles the program).

%1.sqlj
The SQLJ source file.

%1.java
The translated Java file from the SQLJ source file.

db2profc
The DB2 for Java profile customizer.

-url
Specifies a JDBC URL for establishing a database connection, such as jdbc:db2:sample.

-prepoptions
Specifies the package name for the database with the string "package using %1", where %1 is the SQLJ source file name.

%1_SJProfile0
Specifies a serialized profile for the program.

In the following UNIX script file, the first parameter, $1, specifies the name of your source file. The second parameter, $2, specifies the name of the database to which you want to connect. Since the stored procedure must be built on the same instance where the database resides, there are no parameters for user ID and password.

Only the first parameter, the source file name, is required. If no database name is supplied, the program uses the default sample database.

#! /bin/ksh
# bldsqljs script file -- UNIX platforms
# Builds a Java embedded SQL (SQLJ) stored procedure
# Usage: bldsqljs <prog_name> [ <db_name> ]
 
# Set DB2PATH to where DB2 will be accessed.
# The default is the standard instance path.
DB2PATH=$HOME/sqllib
 
# Translate and compile the SQLJ source file
# and bind the package to the database.
if (($# < 2))
then
   sqlj $1.sqlj
   db2profc -url=jdbc:db2:sample -prepoptions="package using $1" $1_SJProfile0
else
   sqlj     -url=jdbc:db2:$2 $1.sqlj
   db2profc -url=jdbc:db2:$2 -prepoptions="package using $1" $1_SJProfile0
fi
 
# Copy the *.class and *.ser files to the 'function' directory.
rm -f $DB2PATH/function/$1*.class
rm -f $DB2PATH/function/$1*.ser
cp $1*.class $DB2PATH/function
cp $1*.ser $DB2PATH/function


Translator and Precompile Options for bldsqljs

sqlj
The SQLJ translator (also compiles the program).

$1.sqlj
The SQLJ source file.

$1.java
The translated Java file from the SQLJ source file.

db2profc
The DB2 for Java profile customizer.

-url
Specifies a JDBC URL for establishing a database connection, such as jdbc:db2:sample.

-prepoptions
Specifies the package name for the database with the string "package using $1", where $1 is the SQLJ source file name.

$1_SJProfile0
Specifies a serialized profile for the program.

Stserver demonstrates PARAMETER STYLE JAVA stored procedures using the JDBC application driver to access a DB2 database. Stored procedures are compiled and stored on a server. When called by a client application, they access the server database and return information to the client application.

To build this stored procedure class with the build file, bldsqljs:

  1. Enter the following command:
       bldsqljs Stserver [ <db_name> ]
    

    where the optional parameter <db_name> allows you to access another database instead of the default sample database.

  2. Next, catalog the stored procedures by running the Stcreate.db2 script on the server. First, connect to the database:
       db2 connect to sample
    

    If the stored procedures were previously cataloged, you can drop them with this command:

       db2 -td@ -vf Stdrop.db2
    

    Then catalog them with this command:

       db2 -td@ -vf Stcreate.db2
    
  3. Then, stop and restart the database to allow the new shared library to be recognized. If necessary, set the file mode for the shared library so the DB2 instance can access it.
  4. Compile and run the Stclient client application to call the stored procedures. See "Client Programs for Stored Procedures".

You can also use the Java makefile to build this program.


[ Top of Page | Previous Page | Next Page ]