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
- Review Java routines.
- Ensure that you have access to a DB2 Version
9 database server, including instances and databases.
- Ensure that the operating system is at a version level that is
supported by DB2 database products.
- 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.
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
- Code the routine logic in Java.
- A routine parameter signature must be implemented using one of
the supported parameter styles. It is strongly recommended that parameter
style JAVA be used for all Java routines.
For more on parameter signatures and parameter implementations see:
- Declare variables in the same manner as is done for Java database applications. Be careful to correctly
use data types that map to DB2 SQL
data types.
For more on data type mapping between DB2 and Java data
types see:
- Include routine logic. Routine logic can consist of any code
supported in the Java programming
language. It can also include the execution of SQL statements in
the same manner as is done in Java database
applications.
For more on executing SQL statements in Java code see:
- If the routine is a procedure and you might want to return a result
set to the caller of the routine, you do not require any parameters
for the result set. For more on returning result sets from Java routines:
- Set a routine return value at the end of the routine.
- 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:
- 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.
- 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.