IBM Books

Application Building Guide


SQLJ Programs

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 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.
sqlj %1.sqlj
 
rem Bind the package to the database.
if "%2" == "" goto case1
if "%3" == "" goto case2
if "%4" == "" goto error
goto case3
:case1
   db2profc -url=jdbc:db2:sample -prepoptions="package using %1" %1_SJProfile0 
   goto continue
:case2
   db2profc -url=jdbc:db2:%2 -prepoptions="package using %1" %1_SJProfile0
   goto continue
:case3
   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 Bind 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 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
# Builds a Java embedded SQL (SQLJ) sample
# Usage: bldsqlj <prog_name> [ <db_name> [ <userid> <password> ]]
 
# Translate and compile the SQLJ source file.
sqlj $1.sqlj
 
# Bind the package to the database.
if (($# < 2))
then
   db2profc -url=jdbc:db2:sample -prepoptions="package using $1" $1_SJProfile0
elif (($# < 3))
then
   db2profc -url=jdbc:db2:$2 -prepoptions="package using $1" $1_SJProfile0
else
   db2profc -url=jdbc:db2:$2 -user=$3 -password=$4 -prepoptions="package using $1" $1_SJProfile0
fi

Translator and Bind 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 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

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

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

   bldsqlj StpCli [ <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 StpCli

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. 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 Builds a Java embedded SQL (SQLJ) stored procedure.
rem Usage: bldsqljs prog_name [ db_name [ userid password ]]
 
if "%1" == "" goto error
 
rem Translate and compile the SQLJ source file.
sqlj %1.sqlj
 
rem Bind the package to the database.
if "%2" == "" goto case1
if "%3" == "" goto case2
if "%4" == "" goto error
goto case3
:case1
   db2profc -url=jdbc:db2:sample -prepoptions="package using %1" %1_SJProfile0 
   goto continue
:case2
   db2profc -url=jdbc:db2:%2 -prepoptions="package using %1" %1_SJProfile0
   goto continue
:case3
   db2profc -url=jdbc:db2:%2 -user=%3 -password=%4 -prepoptions="package using %1" %1_SJProfile0
   goto continue
: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 [ userid password ]]
 
:exit
@echo on


Translator and Bind 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.

-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 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
# bldsqljs script file
# Builds a Java embedded SQL (SQLJ) stored procedure.
# Usage: bldsqljs <prog_name> [ <db_name> [ <userid> <password> ]]
 
# 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.
sqlj $1.sqlj
 
# Bind the package to the database.
if (($# < 2))
then
   db2profc -url=jdbc:db2:sample -prepoptions="package using $1" $1_SJProfile0
elif (($# < 3))
then
   db2profc -url=jdbc:db2:$2 -prepoptions="package using $1" $1_SJProfile0
else
   db2profc -url=jdbc:db2:$2 -user=$3 -password=$4 -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 Bind 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.

-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 profile for the program.

Stp demonstrates an SQLJ stored procedure 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 with the build file, bldsqljs, enter the following command:

   bldsqljs Stp [ <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.

Compile and run the StpCli client application to call the stored procedure. See "Client Programs for Stored Procedures".

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


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]