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. Steps for creating a Java routine include the formulation of the CREATE statement, coding, compilation (translation), and deployment of the Java class to the database server.

Before you begin

  • Review Java routines.
  • Ensure that you have access to the target database.
  • Ensure that the operating system level is supported by the database product.
  • Ensure that the Java development software is at a version level that is supported for Java routine development. Refer to Supported Java routine development software.
  • Ensure a valid Specification of a driver for Java routines development.
  • Authority to execute the CREATE PROCEDURE or CREATE FUNCTION statement.
  • The SQL procedures are implemented internally as packages, which have a different name. You can list package names that are associated with SQL procedures with the following query

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 database server or install the JAR file to the database server.
    For information on how to do this, see:

    It is recommended that you store class files associated with database 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 the database manager searches 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.