从PL/SQL调用Java

Div*_*vas 9 java plsql

任何人都可以帮我这个:我想从Pl/SQL,Oracle RDBMS调用一个java程序,下面是设置

Windows 7机器,Java安装在C:\ Program Files\Java\jdk1.7.0_02上

我创建了一个目录来保存java文件.D:\ Java,里面有一个hello.java文件.

public class Hello
{
  public static String world()
  {
    return "Hello world";
  }
}
Run Code Online (Sandbox Code Playgroud)

编译得很好,.class文件是在同一目录中生成的.

因为我必须使用PL/SQL调用此函数,所以这里是我编写的PL/SQL函数:

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

这是PL/SQL过程:

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;
Run Code Online (Sandbox Code Playgroud)

使用SQL/developer编译函数和过程都很好.

当我尝试运行此过程时:

set serveroutput on;
execute hellow;
Run Code Online (Sandbox Code Playgroud)

以下错误即将发生:

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.
Run Code Online (Sandbox Code Playgroud)

我也将.class文件放在bin文件夹中,但仍然会出现同样的错误.任何人都可以看看这个.

Vin*_*rat 13

您还可以直接在数据库中编译和保存Java源代码,如存储过程:

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

> Java created
Run Code Online (Sandbox Code Playgroud)

调用此函数非常简单,不需要其他设置:

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
Run Code Online (Sandbox Code Playgroud)