Load Java source code in oracle database

By admin

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.
Javajava
1CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED APPS.TEST_JAVA_PKG
2   AS public class TestMsgClass {
3    public static String getMessage(String pStr) throws Exception {
4        String sl = "";
5
6        try {
7            sl = "This message string is coming from java getMessage method. Input string provided was:" + pStr + "";
8        } catch (Exception e) {
9            sl = e.getMessage();
10        }
11
12        return sl;
13
14    }
15}

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

SQL Querysql
1CREATE OR REPLACE FUNCTION xxtest_msg (p_str VARCHAR2)
2   RETURN VARCHAR2
3AS
4   LANGUAGE JAVA
5   NAME 'TestMsgClass.getMessage(java.lang.String)
6return 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.

SQL Querysql
1SET SERVEROUTPUT ON;
2DECLARE
3   msg   VARCHAR2 (2000);
4BEGIN
5   msg := xxtest_msg ('AbCd');
6   DBMS_OUTPUT.put_line ('msg:' || msg);
7END;
This is the output message that is returned from Java class.

Related posts: