The following replaces the entire chapter.
You can develop Java programs to access DB2 databases with the appropriate Java Development Kit (JDK) on Windows NT, Windows 98, Windows 95, and OS/2. The JDK includes Java Database Connectivity (JDBC), a dynamic SQL API for Java.
DB2 JDBC support is provided by the DB2 Client Application Enabler (DB2 CAE). With this support you can build and run JDBC applications and applets. These contain dynamic SQL only, and use a Java call interface to pass SQL statements to DB2.
The DB2 Software Developer's Kit (DB2 SDK) provides support for Java embedded SQL (SQLJ). With DB2 SQLJ support and DB2 JDBC support you can build and run SQLJ applications and applets. These contain static SQL and use embedded SQL statements that are bound to a DB2 database.
The SQLJ support provided by the DB2 SDK includes:
Building and running different types of Java programs requires support from different components of DB2:
For more information on DB2 programming in Java, see "Programming in JDBC". This covers creating and running JDBC applications, applets, stored procedures and UDFs. For information on SQLJ applications, applets, stored procedures and UDFs, see "Embedded SQL for Java (SQLJ) Programming".
For the latest, updated DB2 Java information, visit the Web Page at http://www.software.ibm.com/data/db2/java.
To build Java applications on a supported Windows platform with DB2 JDBC support, you need to install and configure the following on your development machine:
To run DB2 Java stored procedures or UDFs, you also need to update the DB2 database manager configuration on the server to include the path where the JDK is installed on that machine. You can do this by entering the following on the server command line:
db2 update dbm cfg using JDK11_PATH c:\jdk11
where c:\jdk11 is the path where the JDK is installed.
You can check the DB2 database manager configuration to verify the correct value for the JDK11_PATH field by entering the following command on the server:
db2 get dbm cfg
You may want to pipe the output to a file for easier viewing. The JDK11_PATH field appears near the beginning of the output. For more information on these commands, see the Command Reference.
To run Java programs on a supported Windows platform with DB2 JDBC support, the following environment variables are automatically updated when DB2 is installed, to ensure that:
To build SQLJ programs, CLASSPATH is also updated to include the file:
%DB2PATH%\java\sqlj.zip
To run SQLJ programs, CLASSPATH is also updated to include the file:
%DB2PATH%\java\runtime.zip
To build Java applications on OS/2 with DB2 JDBC support, you need to install and configure the following on your development machine:
The JDK must be installed in an HPFS drive to allow long filenames with extensions greater than three characters, such as .java. Your Java working directory must also be on an HPFS drive. If you will be running Java stored procedures or UDFs on an OS/2 server, DB2 must be installed on an HPFS drive on the server in order to allow the stored procedure or UDF .class files to be placed in the %DB2PATH%\function directory.
To run DB2 Java stored procedures or UDFs, you need to update the DB2 database manager configuration on the server to include the path where the JDK is installed on that machine. You can do this by entering the following on the server command line:
db2 update dbm cfg using JDK11_PATH c:\jdk11
where c:\jdk11 is the path where the JDK is installed.
You can check the DB2 database manager configuration to verify the correct value for the JDK11_PATH field by entering the following command on the server:
db2 get dbm cfg
You may want to pipe the output to a file for easier viewing. The JDK11_PATH field appears near the beginning of the output. For more information on these commands, see the Command Reference.
To run Java programs on OS/2 with DB2 JDBC support, the following environment variables are automatically updated when DB2 is installed, to ensure that:
To build SQLJ programs, CLASSPATH is also updated to include the file:
%DB2PATH%\java\sqlj.zip
To run SQLJ programs, CLASSPATH is also updated to include the file:
%DB2PATH%\java\runtime.zip
DB2 provides sample programs, used in the following sections, to demonstrate building and running JDBC programs that exclusively use dynamic SQL, and SQLJ programs that use static SQL. The Java samples are located in the %DB2PATH%\samples\java directory. The directory also contains a README and a makefile. Please see the section "The Java Makefile".
Before modifying or building the sample programs, it is recommended that you copy them from the %DB2PATH%\samples\java directory to a separate working directory.
On OS/2, your working directory must be on an HPFS drive. Since DB2 sample programs are provided on OS/2 to be compatible with FAT drives, filenames have at most a three character extension. To comply with this restriction, Java sample files have been truncated. After copying the Java files to your working directory, you can rename the truncated files by the following commands:
move *.jav *.java move *.htm *.html move *.sql *.sqlj
To run these sample programs, you must first create and populate the sample database by entering:
db2sampl
General Points for Building and Running DB2 Java Programs
db2start
The makefile provided for the Java sample programs is presented below. The makefile will only work if a compatible make executable program is resident on your system in a directory included in your PATH variable. A suitable make utility may be provided by another language compiler. Please read the comment at the beginning of the text of the makefile for more information.
The make commands used to build specific Java sample programs are cited in the sections that follow. There is one change from makefiles normally used for other languages. The make clean command removes all files produced by the java compiler: .java files and core dumps; the make cleanall command removes these files as well as any files produced by the SQLJ translator.
The makefiles for the supported Windows platforms and for OS/2 are identical except for the header information,
for Windows platforms:
# IBM DB2 Universal Database Version 5 # for Windows NT, Windows 98 and Windows 95 # Makefile for DB2 Java samples
and for OS/2:
# IBM DB2 Universal Database Version 5 for OS/2 # Makefile for DB2 java samples
The rest of the makefile is as follows:
# The makefile will only work if a compatible make executable program is # resident on your system in a directory included in your PATH variable. # Such a make utility may be provided by another language compiler. If you # do not have a compatible make utility you cannot use this makefile, but # you can still compile and run these programs by entering the commands # at the command line, as explained in the README. # To build your applications using this makefile, you can use one of # the following commands: # make all - builds all the programs in this directory # make <prog_name> - builds a program designated by <prog_name> # make clean - removes all files produced by the java compiler # from your working directory (such as .class files) # make cleanall - removes all files from your working directory produced # by both the sqlj translator and java compiler. # This file assumes the DB2 instance path is defined by the variable HOME. # It also assumes that DB2 is installed under the DB2 instance. # If these conditions are not true, redefine DB2INSTANCEPATH DB2INSTANCEPATH = $(HOME) # Use the java compiler CC= javac # To connect to another database update the DATASOURCE variable. # User ID and password are optional. If you want to use them, # update TESTUID with your user ID, and TESTPWD with your password. DATASOURCE=sample TESTUID= TESTPWD= COPY = copy ERASE = del # Note: 'all' contains RunCatUdf, which executes the 'java CatUdf' command, # as this must be run before the 'make Udf' command. all : DB2Appl DB2Applt DB2Stp DB2Udf App Applt Stp CatUdf RunCatUdf Udf DropUdf RunCatUdf : java CatUdf clean : $(ERASE) *.class $(ERASE) core cleanall : clean $(ERASE) App.java $(ERASE) Applt.java $(ERASE) Stp.java $(ERASE) CatUdf.java $(ERASE) Udf.java $(ERASE) DropUdf.java $(ERASE) *.ser # Build and run the following JDBC application with these commands: # # make DB2Appl # java DB2Appl # DB2Appl.class : DB2Appl.java DB2Appl : DB2Appl.class $(CC) DB2Appl.java # After following the setup instructions in the README, you can # build and run the following JDBC applet with these commands: # # make DB2Applt # appletviewer DB2Applt.html # DB2Applt.class : DB2Applt.java DB2Applt : DB2Applt.class $(CC) DB2Applt.java # Build and run the following JDBC stored procedure with these commands: # # make DB2Stp # java DB2Stp # DB2Stp.class : DB2Stp.java DB2Stp : DB2Stp.class $(CC) DB2Stp.java $(COPY) DB2StpSample.class $(DB2PATH)\function # Build and run the following JDBC UDF with these commands: # # make DB2Udf # java DB2Udf # DB2Udf.class : DB2Udf.java DB2Udf : DB2Udf.class $(CC) DB2Udf.java $(COPY) DB2UdfSample.class $(DB2PATH)\function # Build and run the following SQLJ application with these commands: # # make App # java App # App.java : App.sqlj sqlj App.sqlj App.class : App.java App_SJProfile0.ser App : App.class $(CC) App.java db2profc -url=jdbc:db2:sample -prepoptions="package using App" App_SJProfile0 # After following the setup instructions in the README, you can # build and run the following SQLJ applet with these commands: # # make Applt # appletviewer Applt.html # Applt.java : Applt.sqlj sqlj Applt.sqlj Applt.class : Applt.java Applt_SJProfile0.ser Applt : Applt.class $(CC) Applt.java db2profc -url=jdbc:db2:sample -prepoptions="package using Applt" Applt_SJProfile0 # Build and run the following SQLJ stored procedure with these commands: # # make Stp # java Stp # Stp.java : Stp.sqlj sqlj Stp.sqlj Stp.class : Stp.java Stp_SJProfile0.ser Stp : Stp.class $(CC) Stp.java db2profc -url=jdbc:db2:sample -prepoptions="package using Stp" Stp_SJProfile0 $(COPY) Stpsrv.class $(DB2PATH)\function $(COPY) Stp_Cursor1.class $(DB2PATH)\function $(COPY) Stp_Cursor2.class $(DB2PATH)\function $(COPY) Stp_SJProfileKeys.class $(DB2PATH)\function $(COPY) Stp_SJProfile0.ser $(DB2PATH)\function # The following SQLJ User-Defined Function application requires three programs. # Build and run the UDF programs with these commands: # # nmake CatUdf # java CatUdf # nmake Udf # java Udf # nmake DropUdf # java DropUdf # # Note: 'java CatUdf' must be executed before 'nmake Udf'. The 'make all' # command calls RunCatUdf, which executes 'java CatUdf', before calling Udf. # CatUdf.java : CatUdf.sqlj sqlj CatUdf.sqlj CatUdf.class : CatUdf.java CatUdf_SJProfile0.ser CatUdf : CatUdf.class $(CC) CatUdf.java db2profc -url=jdbc:db2:sample -prepoptions="package using CatUdf" CatUdf_SJProfile0 Udf.java : Udf.sqlj sqlj Udf.sqlj Udf.class : Udf.java Udf_SJProfile0.ser Udf : Udf.class $(CC) Udf.java db2profc -url=jdbc:db2:sample -prepoptions="package using Udf" Udf_SJProfile0 $(COPY) Udfsrv.class $(DB2PATH)\function DropUdf.java : DropUdf.sqlj sqlj DropUdf.sqlj DropUdf.class : DropUdf.java DropUdf_SJProfile0.ser DropUdf : DropUdf.class $(CC) DropUdf.java db2profc -url=jdbc:db2:sample -prepoptions="package using DropUdf" DropUdf_SJProfile0
DB2Appl demonstrates a dynamic SQL Java application using the JDBC Application driver to access a DB2 database.
Command Line. To build and run this application by commands entered at the command line:
javac DB2Appl.java
to produce the file: DB2Appl.class.
java DB2Appl
makefile. To build this application with the makefile, and then run it:
make DB2Appl
java DB2Appl
DB2Applt demonstrates a dynamic SQL Java applet using the JDBC applet driver to access a DB2 database.
Command Line. To build and run this applet by commands entered at the command line:
param name=port value='6789'
then you would enter:
db2jstrt 6789
javac DB2Applt.java
to produce the file DB2Applt.class.
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 DB2Applt.html
makefile. To build this applet with the makefile, and then run it:
param name=port value='6789'
then you would enter:
db2jstrt 6789
make DB2Applt
As an alternative to steps (2), (6) and (8), 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 DB2Applt.html
DB2Stp demonstrates how to write a dynamic SQL Java stored procedure using the JDBC Application driver to access a DB2 database.
Command Line. To build and run this stored procedure by commands entered at the command line:
javac DB2Stp.java
This will produce the files DB2Stp.class and DB2StpSample.class.
java DB2Stp
makefile. To build this stored procedure with the makefile, and then run it:
make DB2Stp
java DB2Stp
DB2Udf demonstrates implementing dynamic SQL user-defined functions using the JDBC Application driver to access a DB2 database.
Command Line. To build and run this UDF program by commands entered at the command line:
javac DB2Udf.java
This will produce the files DB2Udf.class and DB2UdfSample.class.
java DB2Udf
makefile. To build this UDF program with the makefile, and then run it:
make DB2Udf
java DB2Udf
App demonstrates an SQLJ application that accesses a DB2 database.
Command Line. To build and run this application by commands entered at the command line:
sqlj App.sqlj
This will produce the files App.java and App_SJProfile0.ser.
javac App.java
This will produce the files: App.class, App_Cursor1.class, App_Cursor2.class and App_SJProfileKeys.class.
db2profc -url=jdbc:db2:sample prepoptions="package using App" App_SJProfile0
java App
makefile. To build this application with the makefile, and then run it:
make App
java App
Applt demonstrates an SQLJ applet that accesses a DB2 database.
Command Line. To build and run this applet by commands entered at the command line:
param name=port value='6789'
then you would enter:
db2jstrt 6789
sqlj Applt.sqlj
This will produce the files: Applt.java and Applt_SJProfile0.ser.
javac Applt.java
This will produce the files: Applt.class, Applt_Cursor1.class, Applt_Cursor2.class and Applt_SJProfileKeys.class.
db2profc -url=jdbc:db2:sample -prepoptions="package using Applt" Applt_SJProfile0
Applt.html Applt.class, Applt_Cursor1.class, Applt_Cursor2.class, Applt_SJProfileKeys.class, Applt_SJProfile0.ser
As an Alternative to steps (1), (7) and (9), 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
makefile. To build this applet with the makefile, and then run it:
param name=port value='6789'
then you would enter:
db2jstrt 6789
make Applt
Applt.html, Applt.class, Applt_Cursor1.class, Applt_Cursor2.class, Applt_SJProfileKeys.class, Applt_SJProfile0.ser
As an Alternative to steps (2), (6) and (8), 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
Stp demonstrates an SQLJ stored procedure that accesses a DB2 database.
Command Line. To build and run this stored procedure by commands entered at the command line:
sqlj Stp.sqlj
This will produce the files Stp.java and Stp_SJProfile0.ser.
javac Stp.java
This will produce the files: Stp.class, Stpsrv.class, Stp_Cursor1.class, Stp_Cursor2.class and Stp_SJProfileKeys.class.
db2profc -url=jdbc:db2:sample -prepoptions="package using Stp" Stp_SJProfile0
java Stp
makefile. To build this stored procedure with the makefile, and then run it:
make Stp
java Stp
The %DB2PATH%\samples\java directory includes a UDF application consisting of three SQLJ programs:
CatUdf.
Command Line. To build and run this SQLJ program by commands entered at the command line:
sqlj CatUdf.sqlj
This will produce the files CatUdf.java and CatUdf_SJProfile0.ser.
javac CatUdf.java
This will produce the files CatUdf.class and CatUdf_SJProfileKeys.class.
db2profc -url=jdbc:db2:sample -prepoptions="package using CatUdf" CatUdf_SJProfile0
java CatUdf
makefile. To build and run this SQLJ program with the makefile:
make CatUdf
Udf.
Command Line. To build and run this SQLJ program by commands entered at the command line:
sqlj Udf.sqlj
This will produce the files Udf.java and Udf_SJProfile0.ser.
javac Udf.java
This will produce the files: Udf.class, Udf_Cursor1.class, Udf_Cursor2.class, Udf_Cursor4.class, Udf_Cursor5.class, Udf_SJProfileKeys.class and Udfsrv.class.
| Note: | There is no file Udf_Cursor3.class. |
db2profc -url=jdbc:db2:sample -prepoptions="package using Udf" Udf_SJProfile0
java Udf
makefile. To build this SQLJ program with the makefile, and then run it:
make Udf
java Udf
DropUdf.
Command Line. To build and run this SQLJ program by commands entered at the command line:
sqlj DropUdf.sqlj
This will produce the files DropUdf.java and DropUdf_SJProfile0.ser.
javac DropUdf.java
This will produce the files: DropUdf.class and DropUdf_SJProfileKeys.class.
db2profc -url=jdbc:db2:sample -prepoptions="package using DropUdf"
DropUdf_SJProfile0
java DropUdf
makefile. To build this SQLJ program with the makefile, and then run it:
make DropUdf
java DropUdf
http://www.software.ibm.com/data/db2/db2lotus/gojava.htm