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.
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.
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.
- Ensure a valid Specification of a driver for Java routines development.
- Authority to execute the CREATE PROCEDURE or CREATE FUNCTION statement.
Procedure
- Code the routine logic in Java.
- For general information about Java routines
and Java routine features, see
the topics referenced in the Prerequisites section.
- 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 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 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.
What to do next
To
invoke your Java routine, see
Invoking routines