PL/Java v11
The PL/Java package provides access to Java stored procedures, triggers, and functions via the JDBC interface. Unless otherwise noted, the commands and paths noted in the following section assume that you have performed an installation with the edb-asxx-pljava
RPM package where xx
is the Advanced Server version number.
Before installing PL/Java for use with a standard Java virtual machine (JVM) on a Linux system, you must first confirm that a Java runtime environment (version 1.8) is installed on your system. Installation of a Java development kit also provides a Java runtime environment.
Installing PL/Java on Linux
The following steps outline the process of installing PL/Java on a Linux system:
Step 1: Edit the postgresql.conf
file located under the data
directory of your Advanced Server installation and add (or modify) the following settings:
Where path_to_pljava.jar
specifies the location of the pljava.jar
file and path_to_libjvm.so
specifies the location of the libjvm.so
file.
For example, the following lists the paths for a default installation with Java version 1.8:
Step 2: Restart the database server.
Step 3: You can use the CREATE EXTENSION
command to install PL/Java. To install the PL/Java extension, login to the database in which you want to install PL/Java with the psql or pgAdmin client, and invoke the following command:
Step 4: To confirm that PL/Java is installed, invoke the following command:
The edb-psql
client displays two rows indicating that java
and javau
(Java Untrusted) have been installed in the database.
Installing PL/Java on Windows
The following steps outline the process of installing PL/Java on a Windows system.
Step 1: Edit the postgresql.conf
file and add (or modify) the following settings:
Where POSTGRES_INSTALL_HOME
specifies the location of the Advanced Server installation. For example, the following is the configuration setting for a default installation:
Step 2: Restart the database server.
Step 3: Modify the PATH setting used by the server, adding the following two entries:
Where JRE_HOME
specifies the installation directory of your Java runtime environment. If you have a Java development kit, substitute the location of $JDK_HOME/jre
for JRE_HOME
.
Step 4: Use the Postgres CREATE EXTENSION
command to install PL/Java. To run the installation script, use the psql or pgAdmin client to connect to the database in which you wish to install PL/Java and invoke the following command:
Step 5: To confirm that PL/Java is installed, invoke the following command:
The client will return a result set that includes java and javau (Java Untrusted).
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.
The example that follows demonstrates the procedures used to create and install a simple HelloWorld
program:
Step 1: Save the following code sample to a file named HelloWorld.java
:
Step 2: Compile the file.
And save it in a folder hierarchy as:
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:
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:
You should see the output:
The official PL/Java distribution is distributed with examples and documentation. For more information about using PL/Java, see the project page at:
https://github.com/tada/pljava/wiki
Note
The PL/Java package is deprecated in Advanced Server 11 and will be unavailable in server versions 12 or later.