我们可以将Oracle RECORD TYPE与java Callable语句集成吗?

时间:2013-06-27 09:30:26

标签: java oracle jdbc

RECORD类型与JDBC不兼容是真的吗?司机正在使用的是OJDBC6.jar

Java代码:

dbStrategy.openConnection();
        WrappedConnectionJDK6 wrapped = (WrappedConnectionJDK6) dbStrategy.getConnection();
        OracleConnection oracleConnection = (OracleConnection) wrapped.getUnderlyingConnection();
        CallableStatement callableStatement = oracleConnection.prepareCall("{call XXX_Info_Utl_Pkg.get_order_admin(?,?,?,?,?)}");
        callableStatement.setInt(1, Integer.parseInt(orderNumber));
        callableStatement.registerOutParameter(2, OracleTypes.ARRAY, "XXX_INFO_UTL_PKG.SEAGRS_ORDER_REC");
        callableStatement.registerOutParameter(3, OracleTypes.NUMBER);
        callableStatement.registerOutParameter(4, OracleTypes.VARCHAR);
        callableStatement.registerOutParameter(5, OracleTypes.NUMBER);

和Oracle代码:

PROCEDURE get_order_admin(
            p_order_num_in IN NUMBER,
            p_order_admin_out OUT XXX_info_utl_pkg.seagrs_order_rec,
            p_error_code_out OUT PLS_INTEGER,
            p_error_msg_out OUT VARCHAR2,
            p_ret_status_out OUT PLS_INTEGER);


TYPE seagrs_order_rec
IS
    record
    (
        order_admin        VARCHAR2(10),
        contact_name       VARCHAR2(50),
        contact_email_addr VARCHAR2(100),
        contact_phone_num  VARCHAR2(30));

我面临的错误是 -

  

例外:名称模式无效:XXX_INFO_UTL_PKG.SEAGRS_ORDER_REC

2 个答案:

答案 0 :(得分:3)

记录类型与JDBC驱动程序不兼容:

  

Oracle JDBC驱动程序支持调用是不可行的   PL / SQL RECORD,BOOLEAN或表的参数或返回值   非标量元素类型。但是,Oracle JDBC驱动程序支持   PL / SQL索引 - 标量元素类型表。

READ THIS

答案 1 :(得分:2)

虽然您无法直接使用JDBC来获取PL / SQL RECORD类型,但您可以编写一个匿名PL / SQL块并将该记录返回到本地变量中,然后您可以按如下方式分解其各个属性:

DECLARE
    rec XXX_info_utl_pkg.seagrs_order_rec;
BEGIN
    get_order_admin(?, rec, ?, ?, ?);
    ? := rec.order_admin;
    ? := rec.contact_name;
    ? := rec.contact_email_addr;
    ? := rec.contact_phone_num;
END;

您现在可以使用以下JDBC代码来调用该过程:

try (CallableStatement s = con.prepareCall(" ... previous PL/SQL block ...")) {
    s.setInt(1, Integer.parseInt(orderNumber));

    // Ordinary out parameters
    s.registerOutParameter(2, Types.NUMBER);
    s.registerOutParameter(3, Types.VARCHAR);
    s.registerOutParameter(4, Types.NUMBER);

    // Out parameters from your decomposed record
    s.registerOutParameter(5, Types.VARCHAR);
    s.registerOutParameter(6, Types.VARCHAR);
    s.registerOutParameter(7, Types.VARCHAR);
    s.registerOutParameter(8, Types.VARCHAR);

    s.execute();
    // Now, retrieve all the out parameters
}

I have recently blogged about this technique more in detail。博客文章还包含有关如何为所有具有记录IN / OUT参数的函数/过程自动生成匿名PL / SQL块的说明。