Data Management
Project Management
Archived stuff
Cryptic crossword.
A bit about me.
Read my blog
Recent additions
Home

Oracle Articles

An Introduction to Java Stored Modules in Oracle:

(Edited: 25 April 2005)

Oracle now allows you to create Java stored procedures and functions right in the database. Here's a very quick introduction to this functionality. To run the example you will need the Sun JDK and Oracle 8i or better. The steps involved are

1) Create a Java class containing your stored procedure/function.
2) Compile the class outside of the database using a Java compiler.
3) Load the compiled class into the database using the Oracle loadjava utility.
4) Publish the call spec within Oracle.

The example constructs a Java stored module that returns the error function using a rational approximation.

Java class

public class Stat {
  public static double erf (double x) {

    double a1 = .0705230784;
    double a2 = .0422820123;
    double a3 = .0092705272;
    double a4 = .0001520143;
    double a5 = .0002765672;
    double a6 = .0000430638;
    double xs,xc;

    xs = x*x;
    xc = xs*x;
    return (1.0-1.0/Math.pow(1+a1*x+a2*xs+a3*xc+a4*xs*xs+a5*xc*xs+a6*xc*xc,16));

  }
}

Save the text in a file called Stat.java Then compile the class from the command line using javac. Once this is done, load the class (again from the command line) into the scott/tiger (or any other) schema. Here are the commands:

Compile and Load

javac Stat.java

loadjava -u scott/tiger Stat.class
 

The last step is to publish the call spec. This step, which establishes correspondences between Java and SQL datatypes, allows you to call the function as you would a normal PL/SQL function. Finally you can test that the function works by SELECTing it from dual. Note that the call spec and the test will have to be run within the database database (from SQLPlus or any other tool).

Call Spec and test

create or replace function erf(x number) return number
  as language java
  name 'Stat.erf(double) return double';

select erf(1) from dual;


Note: The rational approximation used here is from p. 299 of A Handbook of Mathematical Functions, M. Abramowitz and I. Stegun (Dover, 1972). Back to the top