gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:30:25 24 4
gpt4 key购买 nike

我可以毫无问题地在 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;
/

最佳答案

/* 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?

关于java - 如何将数组从 Java 返回到 PL/SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7872688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com