DB2 Version 10.1 for Linux, UNIX, and Windows

Creating Java routines from the command line

Procedures and functions that reference a Java™ class are created in a similar way to external routines with other implementations. This task comprises a few steps including the formulation of the CREATE statement for the routine, the coding and compilation (translation) of the routine implementation, and the deployment of the Java class to a DB2® database server.

Before you begin

About this task

You would choose to implement a Java routine if:
  • You want to encapsulate complex logic into a routine that accesses the database or that performs an action outside of the database.
  • You require the encapsulated logic to be invoked from any of: multiple applications, the CLP, another routine (procedure, function (UDF), or method), or a trigger.
  • You are most comfortable coding this logic using Java and one of the JDBC or SQLJ application programming interfaces.

Procedure

  1. Code the routine logic in Java.
  2. Build your code to produce a Java class file or JAR file containing a collection of Java class files. For information on how to build Java routine code, see:
  3. Copy the class file to the DB2 database server or install the JAR file to the DB2 database server. For information on how to do this, see:

    It is recommended that you store class files associated with DB2 routines in the function directory. To find out more about the function directory, see information related to the EXTERNAL clause in one of the following statements: CREATE PROCEDURE or CREATE FUNCTION.

    You can copy the library to another directory on the server if you wish, but to successfully invoke the routine you must note the fully qualified path name of your library as you will require it for the next step.

  4. Execute either dynamically or statically the appropriate CREATE statement for the routine type: CREATE PROCEDURE or CREATE FUNCTION.
    • Specify the LANGUAGE clause with: JAVA
    • Specify the PARAMETER STYLE clause with the name of the supported parameter style that was implemented in the routine code. It is strongly recommended that PARAMETER STYLE JAVA be used unless the features you require are only supported when PARAMETER STYLE DB2GENERAL is used.
    • Specify the EXTERNAL clause with the name of the JAR file or Java class to be associated with the routine using one of the following values:
      • the fully qualified path name of the Java class file
      • the relative path name of the routine Java class file relative to the function directory.
      • the JAR file ID of the JAR file on the database server that contains the Java class

      By default DB2 database systems will look for the library in the function directory unless a JAR file ID and class, fully qualified path name, or relative path name for it is specified in the EXTERNAL clause.

    • Specify DYNAMIC RESULT SETS with a numeric value if your routine is a procedure and it will return one or more result sets to the caller.
    • Specify any other non-default clause values in the CREATE statement to be used to characterize the routine.

What to do next

To invoke your Java routine, see Invoking routines.