Java in Oracle Database Example

  • PLSQL
  • 2 mins read

In this tutorial, I am giving an example to implement Java code in Oracle Database using the stored function. We can create Java programs in the Oracle database by using CREATE OR REPLACE AND COMPILE JAVA SOURCE statement. After that, we can refer that code in a stored function or a stored procedure with AS LANGUAGE JAVA clause. The following are the steps to implement Java in Oracle Database.

Java in Oracle Database Example

  1. The following is the Java Hello World program example. First, we will add Java code using the CREATE OR REPLACE AND COMPILE AS JAVA SOURCE statement in Oracle database.
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Hello" AS
public class Hello
{
public static String World()
{
return "Hello World!";
}
};
/
  1. Now create a stored function referring to the above Java program in Oracle database.
CREATE OR REPLACE FUNCTION helloworld
RETURN VARCHAR2
AS
LANGUAGE JAVA
NAME 'Hello.World () return java.lang.String';
/

DECLARE
v_string VARCHAR2 (100 CHAR);
BEGIN
v_string := helloworld ();
END;
/

You can test it now:

SELECT helloworld FROM DUAL;

Output:

HELLOWORLD 
--------------------------
Hello World! 
1 row selected.

This Post Has 4 Comments

  1. hari satyanarayana

    how to write an import statement in the Oracle java code

    1. Vinish Kapoor

      Please give more details about your question, like what Java code you want to execute and for what purpose?

      I will recommend you to ask your question in our Q&A forum so that other developers also can help you.

  2. ATyant Jain

    I would like to invoke the REST API from PL/SQL and for that I would like to Jar file which i can basically load from LoadJava Utility. I am currently facing issue with calling Java class developed in Eclipse and it fails. Q- Do i need some special privelege or what will be the way to call the class or .jar file form Store Procedure

    1. Vinish Kapoor

      Have you given the following permissions to your user/schema before loading the jar files?

      sqlplus sys/syspsw@orcl as sysdba
      
      call dbms_java.grant_permission('USERNAME', 'java.util.PropertyPermission','*', 'read,write');
      execute dbms_java.grant_permission('USERNAME','java.util.PropertyPermission','*','read');
      execute dbms_java.grant_permission( 'USERNAME', 'SYS:java.lang.RuntimePermission', 'getClassLoader', ' ' );
      execute dbms_java.grant_permission( 'USERNAME', 'SYS:oracle.aurora.security.JServerPermission', 'Verifier', ' ' );
      execute dbms_java.grant_permission( 'USERNAME', 'SYS:java.lang.RuntimePermission', 'accessClassInPackage.sun.util.calendar', ' ' ) ; 
      execute dbms_java.grant_permission( 'USERNAME', 'java.net.SocketPermission', '*', 'connect,resolve' );
      execute dbms_java.grant_permission( 'USERNAME', 'SYS:java.lang.RuntimePermission', 'createClassLoader', ' ');
      

      If not then you can try to execute the above commands by changing the USERNAME with your Oracle schema name.

Comments are closed.