Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Java from PL/SQL

Tags:

java

plsql

Can any one please help me on this: I want to call one java program from the Pl/SQL, Oracle RDBMS, the below are the settings

Windows 7 machine, Java is installed on C:\Program Files\Java\jdk1.7.0_02

I created one directory to keep the java files. D:\Java, it has one hello.java file in it.

public class Hello
{
  public static String world()
  {
    return "Hello world";
  }
}

this was compiled fine, and the .class file was generated in the same directory.

Since I have to call this function using PL/SQL, here is the PL/SQL function I've written:

create or replace
FUNCTION helloworld RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'Hello.world () return java.lang.String';

and this is the PL/SQL procedure:

create or replace
PROCEDURE hellow
AS
  my_string varchar2(400 char);
begin
  my_string:=helloworld();
  dbms_output.put_line('The value of the string is ' || my_string);
end;

both the function and the procedure compiled fine using the SQL/developer.

When I tried running this procedure:

set serveroutput on;
execute hellow;

the following error is coming:

Error starting at line 2 in command: execute hellow Error report: ORA-29540: class Hello does not exist ORA-06512: at "ORACLE_SOURCE.HELLOWORLD", line 1 ORA-06512: at "ORACLE_SOURCE.HELLOW", line 5 ORA-06512: at line 1
29540. 00000 -  "class %s does not exist"  
*Cause:    Java method execution failed to find a class with the indicated name.
*Action:   Correct the name or add the missing Java class.

I placed the .class file in the bin folder also, but still the same error is coming. Can anyone please have a look at this.

like image 839
Divas Avatar asked Sep 03 '13 06:09

Divas


People also ask

Can I use Java in Oracle?

You can access it from any Oracle Net client, such as OCI and PRO*, or JDBC or SQLJ. In addition, you can use Java to develop powerful, server-side programs, which can be independent of PL/SQL. Oracle Database provides a complete implementation of the standard Java programming language and a fully compliant JVM.

How do I access PL/SQL in Java?

The methods that allow Java to access PL/SQL (using the JDBC approach) are contained in an Oracle JDBC “jar” file (“ojdbc5.jar” for Java 1.5, and “ojdbc6.jar” for Java 1.6). Most installations of an Oracle product will probably already have both of these “jar” files tucked away in some subdirectory under Oracle Home.

What does the “+” mean in PL/SQL?

The second line: is fairly self explanatory: double quotes function as single quotes would in PL/SQL, and the “+” in Java represents string concatenation, or “||” in PL/SQL. In PL/SQL, we could write this method as:

How to invoke a Java program in the Oracle Database?

Before you can invoke a Java program in the Oracle database you must upload it to the server. When PL/SQL invokes a Java class' methods it does so in a JVM running on the Oracle server, not on your local system, and the source files must be uploaded to the Oracle system, where they get compiled.

How do I run a Java program in an Oracle Database?

Compile the program using a file-based Java compiler. Load and store the compiled code in an Oracle database schema. Create a PL/SQL wrapper, or interface, for the Java program.


2 Answers

You can also compile and save your java source directly in the database, like stored procedures:

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Hello" AS
public class Hello
{
  public static String world()
  {
    return "Hello world";
  }
};
/

> Java created

Calling this function is straightforward and doesn't need additional settings:

CREATE OR REPLACE
FUNCTION helloworld RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'Hello.world () return java.lang.String';
/

DECLARE
   my_string VARCHAR2(400 CHAR);
BEGIN
   my_string := helloworld();
   dbms_output.put_line('The value of the string is ' || my_string);
END;
/

> The value of the string is Hello world

> PL/SQL procedure successfully completed
like image 90
Vincent Malgrat Avatar answered Nov 15 '22 17:11

Vincent Malgrat


From http://docs.oracle.com/cd/B28359_01/java.111/b31225/chthree.htm

Load the class on the server using the loadjava tool. You must specify the user name and password. Run the loadjava tool as follows:

loadjava -user scott Hello.class
Password: password

Refer the URL for additional info.

like image 29
inquizitive Avatar answered Nov 15 '22 16:11

inquizitive