Use Java SQL routines

You can access Java™ programs from SQL statements and programs. This can be done using Java stored procedures and Java user-defined functions (UDFs).

To use Java SQL routines, complete the following tasks:

  1. Enable SQLJ

    Because any Java SQL routine may use SQLJ, make SQLJ runtime support always available when running Java 2 Software Development Kit (J2SDK). To enable runtime support for SQLJ in J2SDK, add a link to the SQLJ runtime.zip file from your extensions directory. For more information, see Setting up your server to use SQLJ.

  2. Write the Java methods for the routines

    A Java SQL routine processes a Java method from SQL. This method must be written using either the DB2® or SQLJ parameter passing conventions. See Java stored procedures, Java user-defined functions, and Java user-defined table functions for more information about coding a method used by a Java SQL routine.

  3. Compile the Java classes

    Java SQL routines written using the Java parameter style may be compiled without any addition setup. However, Java SQL routines using the DB2GENERAL parameter style must extend either the com.ibm.db2.app.UDF class or com.ibm.db2.app.StoredProc class. These classes are contained in the JAR file, /QIBM/ProdData/Java400/ext/db2routines_classes.jar. When using javac to compile these routines, this JAR file must exist in the CLASSPATH. For example, the following command compiles a Java source file containing a routine which uses the DB2GENERAL parameter style:

         javac -DCLASSPATH=/QIBM/ProdData/Java400/ext/db2routines_classes.jar
         source.java
  4. Make the compiled classes accessible to the Java virtual machine (JVM) used by the database

    The user-defined classes used by the database JVM can either reside in the /QIBM/UserData/OS400/SQLLib/Function directory or in a JAR file registered to the database.

    The /QIBM/UserData/OS400/SQLLib/Function is the iSeries™ equivalent of /sqllib/function, the directory where DB2 UDB stores Java stored procedures and Java UDFs on other platforms. If the class is part of a Java package, it must reside in the appropriate subdirectory. For example, if the runit class is created as part of the foo.bar package, the file runnit.class should be in the integrated file system directory, /QIBM/ProdData/OS400/SQLLib/Function/foo/bar.

    The class file may also be placed in a JAR file that is registered to the database. The JAR file is registered using the SQLJ.INSTALL_JAR stored procedure. This stored procedure is used to assign a JAR ID to a JAR file. This JAR ID is used to identify the JAR file in which the class file resides. See SQLJ procedures that manipulate JAR files for more information on SQLJ.INSTALL_JAR as well as other stored procedures to manipulate JAR files.

  5. Register the routine with the database.
    Java SQL routines is registered with the database using the CREATE PROCEDURE and CREATE FUNCTION SQL statements. These statements contain the following elements:
    CREATE keywords
    The SQL statements to create a Java SQL routine begin with either CREATE PROCEDURE or CREATE STATEMENT.
    Name of routine
    The SQL statement then identifies the name of the routine that is known to the database. This is the name that is used to access the Java routine from SQL.
    Parameters and return value
    The SQL statement then identifies the parameters and return values, if applicable, for the Java routine.
    LANGUAGE JAVA
    The SQL statement uses the keywords LANGUAGE JAVA to indicate that the routine was written in Java.
    PARAMETER STYLE KEYWORDS
    The SQL statement then identifies the parameter style using the keywords PARAMETER STYLE JAVA or PARAMETER STYLE DB2GENERAL.
    External name
    The SQL statement then identifies the Java method to be processed as Java SQL routines. The external name has one of two formats:
    • If the method is in a class file that is located under the /QIBM/UserData/OS400/SQLLib/Function directory, then the method is identified using the format classname.methodname, where classname is the fully qualified name of the class and methodname is the name of the method.
    • If the method is in a JAR file registered to the database, then the method is identified using the format jarid:classname.methodname, where jarid is the JAR ID of the registered JAR file, classname is the name of the class, and methodname is the name of the method.

    The iSeries Navigator may be used to create a stored procedure or user-defined function that uses the Java parameter style.

  6. Use the Java procedure

    A Java stored procedure is called using the SQL CALL statement. A Java UDF is a function that is called as part of another SQL statement.

Related concepts
Java stored procedures
Java user-defined scalar functions
SQLJ procedures that manipulate JAR files
Parameter passing conventions for Java stored procedures and UDFs