Load Java source code in oracle database

Load Java source code in oracle database
CREATE OR REPLACE AND RESOLVE JAVA SOURCE
Compile java code in oracle database
Call java in oracle apps database procedure
writing java code in oracle database
create java code in oracle database

Below is a sample java class that we will compile and call from database function

Step 1: Compile the below java class code in oracle database. Please note that java should be already installed and configured on server else it will throw “ORA-29538: Java not installed” error.
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED APPS.TEST_JAVA_PKG
   AS public class TestMsgClass {
    public static String getMessage(String pStr) throws Exception {
        String sl = "";

        try {
            sl = "This message string is coming from java getMessage method. Input string provided was:" + pStr + "";
        } catch (Exception e) {
            sl = e.getMessage();
        }

        return sl;

    }
}

Step 2: Compile the below code in oracle database. This function calls the java class that we compiled in step 1.

CREATE OR REPLACE FUNCTION xxtest_msg (p_str VARCHAR2)
   RETURN VARCHAR2
AS
   LANGUAGE JAVA
   NAME 'TestMsgClass.getMessage(java.lang.String)
return java.lang.String' ;

Step 3: Run the below anonymous block to call the function which calls java class.
It should return a message as seen in screenshot below.

SET SERVEROUTPUT ON;
DECLARE
   msg   VARCHAR2 (2000);
BEGIN
   msg := xxtest_msg ('AbCd');
   DBMS_OUTPUT.put_line ('msg:' || msg);
END;
This is the output message that is returned from Java class.

Related posts: Upload your own post and refer it anywhere anytime:

Leave a Reply