如何将数组参数从PL / SQL传递给Java

时间:2013-10-20 17:30:11

标签: java arrays plsql

我是java的新手,并试图学习将java类加载到oracle 11g-r2,然后从PL / SQL调用。我可以使用“loadjava”然后从PL / SQL w / String参数调用并接收String类型的返回值。但是当谈到数组时,我读了之前的两篇文章:“如何将数组从Java返回到PL / SQL?”和“如何创建一个oracle.sql.ARRAY对象?”并使用代码获取“从Java返回数组到PL / SQL”工作。当传递数组参数时,我得到了“PLS-00306:调用的错误数量或类型的参数...”。非常感谢您的评论。这是用于返回数组的代码(和输出) -

create or replace type icrp_data_varray is table of number(5);
/
create or replace and compile java source named "dbICRep" as
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
import java.util.*;
public class dbICRep
{
    public static oracle.sql.ARRAY selectICRep() throws java.sql.SQLException
    {
        List<Double> dData = Arrays.asList(50.0,50.0,50.0,50.0,50.0);
        double[] dml = new double[5];
        int iCount = 0;
        for (ListIterator<Double> it = dData.listIterator(); it.hasNext();)
        {
            dml[iCount] = it.next();
            iCount++;
        }
        OracleDataSource ds = new OracleDataSource();
        ds.setURL("jdbc:default:connection");
        OracleConnection oraConn = (OracleConnection)ds.getConnection();
        oracle.sql.ARRAY dmlArray = oraConn.createARRAY("ICRP_DATA_VARRAY", dml);      
        return dmlArray;
    }
};
/
show errors java source "dbICRep"

create or replace function selectICRepPer return icrp_data_varray as
language java name 'dbICRep.selectICRep() return java.sql.Array';
/
show errors

set serveroutput on

declare
  icrp constant icrp_data_varray := selectICRepPer;
begin
  for i in icrp.first .. icrp.last loop
    dbms_output.put_line('icrp(' || i || ') = ' || icrp(i));
  end loop;
end;
/

icrp(1) = 50
icrp(2) = 50
icrp(3) = 50
icrp(4) = 50
icrp(5) = 50

嗨,我看到了Przemyslaw Kruglej和Bob Jarvis的回复,谢谢。我正在努力添加/回复w /代码无法正常工作。所以,我只是应用了与String参数相同的形式,我真的不应该期望这样做 -

create or replace type icrp_key_varray is table of integer(8);
/
create or replace type icrp_data_varray is table of number(5);
/
create or replace and compile java source named "dbICRep" as
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
import java.util.*;
public class dbICRep
{
    public static oracle.sql.ARRAY selectICRep(oracle.sql.ARRAY keys) throws java.sql.SQLException
    {
        List<Integer> dKeys = new ArrayList<Integer>();
        for (int iCount = 0; iCount < keys.getIntArray().length; iCount++)
        {
            dKeys.add(keys.getIntArray()[iCount]);
        }

        List<Double> dData = Arrays.asList(50.0,50.0,50.0,50.0,50.0);
        double[] dml = new double[5];
        int iCount = 0;
        for (ListIterator<Double> it = dData.listIterator(); it.hasNext();)
        {
            dml[iCount] = it.next();
            iCount++;
        }
        OracleDataSource ds = new OracleDataSource();
        ds.setURL("jdbc:default:connection");
        OracleConnection oraConn = (OracleConnection)ds.getConnection();
        oracle.sql.ARRAY dmlArray = oraConn.createARRAY("ICRP_DATA_VARRAY", dml);      
        return dmlArray;
    }
};
/
show errors java source "dbICRep"

create or replace function selectICRepPer(keys IN icrp_key_varray) return icrp_data_varray as
language java name 'dbICRep.selectICRep(oracle.sql.ARRAY) return oracle.sql.ARRAY';
/
show errors

set serveroutput on

declare
    icrp icrp_data_varray;
begin
    icrp := selectICRepPer(4,124,17,18,1,0,0,2);
    for i in icrp.first .. icrp.last loop
    dbms_output.put_line('icrp(' || i || ') = ' || icrp(i));
    end loop;
end;
/

我在复制/剪切/粘贴期间犯了很多错误,其中,PL / SQL匿名块完全搞砸了,“声明”部分应包括:     keys icrp_key_varray:= icrp_key_varray(4,124,17,18,1,0,0,2); 函数调用应该是:     icrp:= selectICRepPer(keys); 但最糟糕的是:它现在有效!浪费你的时间,我感到非常尴尬和非常抱歉!

0 个答案:

没有答案