Monday 26 May 2014

Invoke A Java Method From A Pl/Sql Function

The below steps describes how to Invoke a Java method from Pl/Sql Function.

  • Step 1 : Create the java method as a static method in the class.

Create the following java file as CallJavaDemo.java 
public class CallJavaDemo {    public static String greetings(String s)    {  return "Hello  "+s;}}

  • Step 2 : Move the Java file to a any location on the oracle database server.
Login to the Oracle database server using 'oracle' user from any FTP s/w. Transfer the java file to any location for example /home/oracle.
  • Step 3 : Compile the Java file using  'javac' command.
Login to the Oracle database server using 'oracle' user from any putty s/w. Execute the below commands to compile the java file. It will generate CallJavaDemo.class file. 
cd /home/oracle
javac  CallJavaDemo.java
  • Step 4 : Load the class file into oracle database server using 'loadjava' utility.
Run the below command using the connection string of the Oracle database.
loadjava -resolve -user username/password@localhost:1521:SID CallJavaDemo.class
  • Step 5 : Create a plsql function with mapping to the loaded java class method. Here the plsql function should have same signature as of the java method.
create or replaceFUNCTION greetings( a in varchar2) RETURN VARCHAR2 AS    LANGUAGE JAVA NAME 'CallJavaDemo.greetings(java.lang.String) return java.lang.String';

  • Step 6 : Test the plsql function. 













Give your feedback on this post. Happy Coding :) 

No comments:

Post a Comment