如何将数组从Java返回到PL / SQL?

时间:2011-10-24 07:57:29

标签: java oracle plsql oracle11g

将PL / SQL中的数字和字符串来回传递给Java没有问题,但是如何传递数组?我从PL / SQL调用Java - 而不是其他方式。

以下是get_widgets_as_string按预期工作的示例。如何为so19j.get_widgets_as_array()编写PL / SQL 调用规范,以便我可以从PL / SQL调用它?

我已阅读Publishing Java Classes With Call Specifications,其中我可以看到嵌套表对应oracle.sql.ARRAY,但我无法使其正常工作。我可能错过了一些微不足道的细节,因为我不是Java程序员。

create or replace and compile java source named "so19j" as

import java.lang.*;

public class so19j {
    public static String get_widgets_as_string() {
        String widgets = "foo;bar;zoo";
        return widgets;
    }

    public static String[] get_widgets_as_array() {
        String[] widgets = new String[]{"foo", "bar", "zoo"};
        return widgets;
    }
};
/
show errors java source "so19j"

create or replace function get_widgets_as_string return varchar2 as
language java name 'so19j.get_widgets_as_string() return java.lang.String';
/
show errors

declare
  widgets constant varchar2(32767) := get_widgets_as_string;
begin
  dbms_output.put_line('widgets = ' || widgets);
end;
/

/* How to write a call specification for so19j.get_widgets_as_array so that it
can be excercised by the PL/SQL block below ? */

declare
  type widgets_t is table of varchar2(32767);
  widgets constant widgets_t := get_widgets_as_array;
begin
  for i in widgets.first .. widgets.last loop
    dbms_output.put_line('widgets(' || i || ') = ' || widgets(i));
  end loop;
end;
/

2 个答案:

答案 0 :(得分:2)

/* The type has to be SQL type so that it is also visible for Java. */
create or replace type widgets_t is table of varchar2(32767);
/

create or replace and compile java source named "so19ja" as
import java.lang.*;
public class so19ja {
    public static String[] get_widgets_as_array() {
        String[] widgets = new String[]{"foo", "bar", "zoo"};
        return widgets;
    }

    public static java.sql.Array array_wrapper(
        String typeName,
        Object elements
    ) throws java.sql.SQLException {
        oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
        java.sql.Connection conn = ora.defaultConnection();
        oracle.jdbc.OracleConnection oraConn =
            (oracle.jdbc.OracleConnection)conn;
        /* Yeah - typeName have to be UPPERCASE, really. */
        java.sql.Array arr = 
            oraConn.createARRAY(typeName.toUpperCase(), elements);
        return arr;
    }

    public static java.sql.Array get_widgets_as_array_wrapped()
    throws java.sql.SQLException {
        return array_wrapper("widgets_t", get_widgets_as_array());
    }
};
/
show errors java source "so19ja"

create or replace function get_widgets_as_array return widgets_t as
language java name 'so19ja.get_widgets_as_array_wrapped() return java.sql.Array';
/
show errors

declare
  widgets constant widgets_t := get_widgets_as_array;
begin
  for i in widgets.first .. widgets.last loop
    dbms_output.put_line('widgets(' || i || ') = ' || widgets(i));
  end loop;
end;
/

打印:

widgets(1) = foo
widgets(2) = bar
widgets(3) = zoo

PL/SQL procedure successfully completed.

另请参阅:How to create an oracle.sql.ARRAY object?

答案 1 :(得分:0)

我不知道pl / sql的事情,但你可以做的是处理PL / SQL的结果并将结果插入到set方法中,然后你就可以得到它。