从Java中为Oracle类型TABLE调用存储过程

时间:2013-11-20 15:24:12

标签: java oracle stored-procedures

我在Oracle中有一个存储过程,如下所示。

create or replace TYPE T_EMP_OBJ AS OBJECT (Name varchar2(20), Age CHAR(3 CHAR));

create or replace type T_EMP_REC as table of T_EMP_OBJ;

create or replace PROCEDURE P_EMP (I_NAV_REC IN T_INAV_REC, p_out OUT MY_TYPES_PKG.REFCURSOR) 
AS  ... 
END P_EMP;

我从java调用该过程。我的代码是,

StructDescriptor structDesc = new StructDescriptor("T_EMP_OBJ", oracleConnection);
ArrayDescriptor arrayDesc2 = new ArrayDescriptor("T_EMP_REC", oracleConnection);
    Object[] objType = new Object[2];
    objType[0] = new String("AAA");
    objType[1] = new String("25");
    STRUCT struct = new STRUCT(structDesc, oracleConnection, objType);
    STRUCT[] structs = new STRUCT[1];
    structs[0] = struct;
    oracle.sql.ARRAY array = new oracle.sql.ARRAY(arrayDesc2, oracleConnection, structs);
    OracleCallableStatement statement = (OracleCallableStatement) oracleConnection.prepareCall("CALL P_EMP(?,?)");
    statement.setArray(1, array);
    statement.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
    statement.execute();
    ResultSet results = ((OracleCallableStatement) statement).getCursor(2);
    while (results.next()) {
         logger.info("NAME==>"+results.getString(1));
         logger.info("AGE===>"+results.getString(2));
    }

虽然我收到记录,但该值为空。这意味着如果输出是2行,则while条件执行并打印null。在SQL Developer中它运行正常。提前谢谢。

1 个答案:

答案 0 :(得分:2)

终于找到了答案here。在类路径中包含orai18n.jar后,它工作正常。