DB2 Universal Database supports many types of Java programs. It provides driver support for client applications and applets written in Java using Java Database Connectivity (JDBC(TM)). It also provides support for embedded SQL for Java (SQLj), Java user-defined functions (UDFs), and Java stored procedures.
The DB2 JDBC application (Type 2) driver (Figure 1) enables Java applications to make calls to DB2 through JDBC. Calls to the JDBC application driver are translated to Java native methods. The Java applications that use this driver must run on a DB2 client, through which JDBC requests flow to the DB2 server.
Figure 1. DB2 Java application implementation
![]() |
The DB2 JDBC applet (Type 3) driver (Figure 2) consists of a JDBC client and a JDBC server. The DB2 JDBC applet driver is loaded by the Web browser along with the applet. When the applet requests a connection to a DB2 database, the applet driver opens a TCP/IP socket to the DB2 JDBC applet server on the machine where the Web server and DB2 client are running. After a connection is set up, the applet driver sends each of the subsequent database access requests from the applet to the JDBC server through the TCP/IP connection. The JDBC server then makes corresponding DB2 calls to perform the task. Upon completion, the JDBC server sends the results back to the JDBC client through the connection.
Figure 2. DB2 Java applet implementation
![]() |
JDBC 1.0 defines the Java APIs for access to relational databases. With the introduction of JDBC 2.0, the APIs have been split into two parts:
The DB2 JDBC 2.0 driver supports the following features of the JDBC 2.0 Optional Package API:
The JNDI and connection pooling interfaces are defined in the JDBC driver and their implementation is provided by WebSphere Application Server, Version 3.5.
Connection pooling is defined as part of the JDBC 2.0 Optional Package API. JNDI and DataSource objects are provided as an alternative to using DriverManager objects to access relational data servers.
JDBC 2.0 support is available on the AIX(R), Solaris(TM), HP-UX, Linux, Linux for OS/390(R), and Windows(R) 32-bit operating systems. To take advantage of the new features of JDBC 2.0, you must install both the JDBC 2.0 driver and the Java Development Kit (JDK(TM)) 1.2, as the JDBC 1.2 driver is the default driver. To install the JDBC 2.0 driver, enter the usejdbc2 command from the sqllib/java12 directory. To switch back to the JDBC 1.2 driver, execute the usejdbc1 command from the sqllib/java12 directory.
For more detail, see:
DB2 SQLj support enables you to build and run SQLj applets and applications. These Java programs contain embedded SQL statements that are precompiled and bound to a DB2 UDB database. SQLj applications use JDBC support, and require the SQLj run-time classes to authenticate and execute any SQL packages that were bound to the database at the precompiling and binding stage.
The SQLj standard has three components: embedded SQLj, a translator, and a run-time environment. The translator translates SQLj files to produce Java source files and profiles. The run-time environment performs the SQL operations in JDBC, and uses the profile to obtain details about database connections.
You can create and use stored procedures in Java just as you would in other languages, with only a few minor differences. After you code the stored procedure, register it with the database by using the CREATE PROCEDURE statement. You can then call the stored procedure from your application. The stored procedure can be FENCED or NOT FENCED.
You can also create and use UDFs in Java just as you would in other languages, with only a few minor differences. After coding the UDF, register it with the database using the CREATE FUNCTION statement. You can then refer to the UDF in the SQL of your application. The UDF can be FENCED or NOT FENCED.
You can use the DB2 Stored Procedure Builder (SPB) to develop Java stored procedures. To help you create stored procedures, DB2 SPB provides design assistants (wizards) that guide you through basic design patterns, help you create SQL queries, and estimate the performance cost of invoking a stored procedure.
DB2 SPB is implemented with Java, and all database connections are managed using JDBC. Using a JDBC driver, you can connect to any local DB2 alias or any other DB2 UDB database for which you can specify a host, port, and database name. Several JDBC drivers are installed with DB2 SPB for use on different operating systems.
To run your UDFs and stored procedures on the server, DB2 invokes the Java Virtual Machine (JVM). Before starting up the database, your database administrator must install and configure the appropriate JDK on your DB2 server. You can choose to use individual Java class files for your stored procedures and UDFs, or collect the class files into a Java Archive (JAR) file and install the JAR file in the database.
The JVM is loaded on the first call to a Java UDF or stored procedure. In all cases, the JVM stays loaded until the embedding process ends. For NOT FENCED UDFs and stored procedures, one JVM is loaded per database instance, and runs inside the database engine's address space for best performance. For FENCED UDFs, a distinct JVM is loaded inside the db2udf process; similarly, FENCED stored procedures load a distinct JVM inside the db2dari process.