从 PL/SQL 调用 Java [英] Calling Java from PL/SQL

查看:20
本文介绍了从 PL/SQL 调用 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 机器,Java 安装在C:Program FilesJavajdk1.7.0_02

Windows 7 machine, Java is installed on C:Program FilesJavajdk1.7.0_02

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

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";
  }
}

编译没问题,.class文件在同一个目录下生成.

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

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

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';

这是 PL/SQL 过程:

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;

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

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

当我尝试运行此程序时:

When I tried running this procedure:

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 文件夹中,但仍然出现相同的错误.任何人都可以看看这个.

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.

推荐答案

你也可以像存储过程一样直接在数据库中编译和保存你的java源代码:

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

这篇关于从 PL/SQL 调用 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆