将对象数组从PLSQL传递到Java函数时出错

时间:2016-02-16 00:11:28

标签: java oracle plsql java-stored-procedures varray

我有一个类型abc_type和一个类型为abc_table的数组。 我正在尝试将对象数组传递给java函数。

create type abc_type authid definer as object
  ( file_name    varchar2(5000),
    file_size    number,
    file_paths   varchar2(4000)
    );
create type abc_table as table of abc_type;

create or replace and compile java source named "Hello" as
public class Hello
{
  public static String world(String str,Array str2)
  {
    return "Hello world - "+ str;
  }
}
/

CREATE OR REPLACE FUNCTION helloworld (
str_in in varchar2,
str2_in in abc_table
)
RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'Hello.world (java.lang.String,oracle.sql.Array) return java.lang.String';
/

declare 
str varchar2(200):='def';
zipfiles abc_table:=abc_table();
begin
zipfiles.extend(1);
zipfiles(1) := abc_type('aaa',22,'bbb');
dbms_output.put_line('test:'||helloworld('abc',zipfiles));
end;
/

所有内容编译都很好,但我收到错误 ORA-29541:class .Hello无法解析。 如果我用String / Varchar2替换Array类型,它可以正常工作。

标题

2 个答案:

答案 0 :(得分:0)

您应该使用import oracle.sql。 ARRAY

create or replace  and COMPILE java source named "Hello" as
import oracle.sql.ARRAY;
public class Hello
{
  public static String world(String str,ARRAY str2)
  {
    return "Hello world - "+ str;
  }
}

我不认为您可以将复合类型(嵌套表)从PL / SQL传递给java函数。在您的情况下,您传递的是复合数据类型的abc_type。据我所知,只能传递一维或单一数据类型的嵌套表或数组。我不太确定,请考虑我的想法。

请检查以下链接 https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:8908169959941

答案 1 :(得分:0)

修正了以下更改

添加了导入语句

import oracle.sql.*; // I had added this earlier, but this alone didn't fix the issue.
import java.sql.*;

//With the above, I was able to pass the object, but I was not able to reference the object.

添加了异常处理。

throws IOException,java.sql.SQLException to the function.

最终代码(还有其他一些更改)

create or replace and compile java source named "Hello" as
package abc;

    import oracle.sql.*;
    import java.sql.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.zip.*;
    import java.sql.Connection;

    public class Hello extends Object
    {
      public static void world(String str,String[] strout, oracle.sql.ARRAY arr) throws IOException,java.sql.SQLException
      {
          Datum[] ZipTyp = ((ARRAY)arr).getOracleArray();
          strout[0] = "Output - "+ str + ","+arr.getSQLTypeName()+ ","+ java.lang.reflect.Array.getLength(ZipTyp);
      }
    }
    /

    CREATE OR REPLACE PROCEDURE helloworld2 (
    str_in IN varchar2,
    str_out OUT varchar2,
    arr_in in abc_table
    )AS
    LANGUAGE JAVA 
    NAME 'abc.Hello.world (java.lang.String,java.lang.String[],oracle.sql.ARRAY)';
    /

    declare 
      str varchar2(200):='def';
      zipfilelist abc_table := abc_table();
      l_in v_table := v_table();
    begin
      zipfilelist.extend(2);
      zipfilelist(1) := abc_type('aaa1',11,'bbb1');
      zipfilelist(2) := abc_type('aaa2',22,'bbb2');
      helloworld2('abc',str,zipfilelist);
      dbms_output.put_line('test:'||str);
    end;
    /