11

任何人都可以帮我解决这个问题:我想从 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";
  }
}

这编译得很好,并且在同一目录中生成了 .class 文件。

由于我必须使用 PL/SQL 调用此函数,因此这是我编写的 PL/SQL 函数:

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

这是 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;

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

当我尝试运行此过程时:

set serveroutput on;
execute hellow;

出现以下错误:

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.

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

4

3 回答 3

17

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

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

> Java created

调用这个函数很简单,不需要额外的设置:

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
于 2013-09-03T06:55:45.360 回答
3

在您可以调用 Oracle 数据库中的 Java 程序之前,您必须将它上传到服务器。当 PL/SQL 调用 Java 类的方法时,它是在Oracle 服务器上运行的 JVM 中调用的,而不是在您的本地系统上,并且源文件必须上传到 Oracle 系统,在那里进行编译。

使用该loadjava工具,如Oracle 上的 Java 应用程序中所述

于 2013-09-03T06:35:48.513 回答
1

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

使用 loadjava 工具在服务器上加载类。您必须指定用户名和密码。运行loadjava工具如下:

loadjava -user scott Hello.class
Password: password

有关其他信息,请参阅 URL。

于 2013-09-03T06:37:03.323 回答