Here is a code example that shows how to implement a Java method for a scalar UDF that maps an SQL INTEGER type parameter to the Java int primitive. This UDF does not handle NULL as an input argument, nor can it return NULL as a return value.
public class UDFExample { public static int fact( int x ) { if (x < 0) return 0; int factResult = 1; while (x > 1) { factResult = factResult * x; x = x - 1; } return factResult; } ... }
If the JAR file for the UDFExample class is called UDFExample.jar, the following statement registers UDFExample.jar and the UDFExample class with the database, and creates an SQL identifier called JarUDF for the JAR file:
CALL SQLJ.INSTALL_JAR('CJ!udfsrc/UDFExample.jar','JarUDF',0);
The corresponding CREATE FUNCTION statement to define the UDF looks like this :
CREATE FUNCTION factorial (x INTEGER) RETURNS INTEGER LANGUAGE JAVA NO SQL PARAMETER STYLE JAVA RETURNS NULL ON NULL INPUT EXTERNAL NAME 'JarUDF:UDFExample.fact';