从 PL/SQL 调用 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18585546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:16:34  来源:igfitidea点击:

Calling Java from PL/SQL

javaplsql

提问by Divas

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

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

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

Windows 7 机器,Java 安装在 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.

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

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

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

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

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

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

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

and this is the PL/SQL procedure:

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

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

函数和过程都使用 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.

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

采纳答案by Vincent Malgrat

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

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

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

回答by Jim Garrison

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.

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

Use the loadjavatool, as described in Java Applications on Oracle

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

回答by inquizitive

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

来自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 工具在服务器上加载类。您必须指定用户名和密码。运行 loadjava 工具,如下所示:

loadjava -user scott Hello.class
Password: password

Refer the URL for additional info.

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