Example: Executing a Java Scalar UDF

Java scalar UDF code example.

  1. Create the Java class.
    public class Sample {
    	public static int add( int a, int b ){
    		return a + b;
    	}
    }
    public class Sample {
    	public static int add( int a, int b ){
    		return new java.lang.Integer(a + b);
    	}
    }
    
  2. Execute the SQL statement to deploy the Java class to the database.
    INSTALL JAVA NEW FROM FILE 'd:\\java\\samples\\Sample.class'
  3. Create the SQL function that maps to the Java method “Sample.add( int, int)”.
    CREATE FUNCTION sample_add_int (IN a int, IN b int)
    RETURNS int
    EXTERNAL NAME 'Sample.add(II)I'
    LANGUAGE JAVA
  4. Use the SQL function in a SELECT statement.
    SELECT sample_add_int( ID, ID ) from Customers WHERE ID < 110
  5. Remove a Java class from the database.
    REMOVE JAVA CLASS ‘Sample’
  6. Update a java class in the database.
    INSTALL JAVA UPDATE FROM FILE 'd:\\java\\samples\\Sample.class'
    INSTALL JAVA JAR UPDATE FROM FILE 'd:\\java\\samples\\Sample.jar'