Table of Contents Previous Next


10 PL/Java : 10.3 Using PL/Java

To create a PL/Java program, you must first create a Java class that contains at least one static method, and then you must compile that class into a .class or .jar file. Next, you declare the Java function within SQL using the CREATE FUNCTION command. The CREATE FUNCTION command gives a SQL name to the function and associates the compiled class (and method name) with that function name.
For example, the following CREATE FUNCTION statement creates a function named getsysprop:
When invoked, getsysprop will execute the getProperty (static) method defined within the java.lang.System class.
Step 1: Save the following code sample to a file named HelloWorld.java:
Step 2: Compile the file.
Step 3: Create an archive file (a JAR file) named helloworld.jar:
Step 4: Open the edb-psql client, and install the jar file with the following command:
SELECT sqlj.install_jar('file:///file_path/helloworld.jar', 'helloworld', true);
Where file_path is the directory containing the helloworld.jar file. For example, if the /tmp directory is the file_path:
To confirm that the jar file has been loaded correctly, perform a SELECT statement on the sqlj.jar_entry and sqlj.jar_repository tables.
Step 5: Set the classpath as:
The sqlj.classpath_entry table will now include an entry for the helloworld class file.
Step 6: Create a function that uses Java to call the static function declared in the jar file:
Step 7: Execute the function:

10 PL/Java : 10.3 Using PL/Java

Table of Contents Previous Next