【问题标题】:Accessing %ROWTYPE of Stored Procedure from Java从 Java 访问存储过程的 %ROWTYPE
【发布时间】:2014-11-26 06:56:22
【问题描述】:

我有一个看起来像的存储过程:

 PROCEDURE get_curx(    p_buf     IN  ni_imsi%ROWTYPE,
                        p_bufx    IN  ni_imsi%ROWTYPE,
                        p_cur     OUT CurTyp,
                        p_where   IN  VARCHAR2 DEFAULT '',
                        p_orderby IN  VARCHAR2 DEFAULT '',
                        p_max     IN  NUMBER   DEFAULT 0,
                        p_lock    IN  NUMBER   DEFAULT 0,
                        p_hint    IN  VARCHAR2 DEFAULT 'NONE',
                        p_debug   IN  NUMBER   DEFAULT 0,
                        p_count   IN  BOOLEAN  DEFAULT FALSE);

我在这样的 JAVA 程序中调用这个过程

CallableStatement cs = connection.prepareCall("{call ni_imsi_pkg.get_curx(?,?,?,?,?,?)}");

            cs.setObject( 1, ? );  // i have no clue what to mention here
            cs.setObject( 2, ? );  //i have no clue what to mention here

            cs.registerOutParameter(3, OracleTypes.CURSOR);
            cs.setString(4, " WHERE current_state = 60000 AND rownum <= 2 ");
            cs.setString(5, " ORDER BY imsi_number");
            cs.setInt(6, 5);

但我不知道如何设置前两个参数。请帮助我。谢谢

【问题讨论】:

  • 你想在这些变量中添加什么,或者更确切地说它们来自哪里以及它们在 Java 端是什么类型的对象?
  • 我怀疑你可以从 JDBC 调用它。 RowType 是特定于 PL/SQL 的。它可能是在某个地方的包中定义的。您可能需要在 PL/SQL 中创建/调用一个帮助程序,该程序使用适当的参数为您调用此类。

标签: java oracle jdbc plsql


【解决方案1】:

正如 Mike 所说,您不能在 JDBC 调用中直接引用行类型,因为行类型仅在 PL/SQL 中有效,并且驱动程序使用的所有类型都必须在 SQL 级别定义。

您可以定义自己的 SQL 对象类型来隐藏您的表结构(如果表被更改,您必须记住更新它),以及采用该类型并将其转换为对您的真实的调用的包装过程程序。这是一个基于对偶的演示,因为我不知道你的真实表结构:

create type ni_imsi_rowtype as object (dummy varchar2(1)) -- use your real table's columns/types
/

create package ni_imsi_pkg as
  procedure get_curx(p_buf dual%rowtype, p_cur out sys_refcursor);
  procedure get_curx_wrapper(p_buf ni_imsi_rowtype, p_cur out sys_refcursor);
end ni_imsi_pkg;
/

create package body ni_imsi_pkg as
  -- original procedure, simplified for demo
  procedure get_curx(p_buf dual%rowtype, p_cur out sys_refcursor) is
  begin
    open p_cur for select * from dual where dummy = p_buf.dummy;
  end;

  -- wrapper procedure taking new type instead of rowtype
  procedure get_curx_wrapper(p_buf ni_imsi_rowtype, p_cur out sys_refcursor) is
    l_buf dual%rowtype;
  begin
    l_buf.dummy := p_buf.dummy;
    get_curx(l_buf, p_cur);
  end;
end ni_imsi_pkg;
/

然后在 Java 端,您可以填充并将其作为 STRUCT 发送:

// Object array containing the values corresponding to your row type
Object[] rowObj = { "X" };
// Struct based on the SQL type you created
StructDescriptor structDesc = StructDescriptor.createDescriptor("NI_IMSI_ROWTYPE", conn);
STRUCT rowStruct = new STRUCT(structDesc, conn, rowObj);

// Call wrapper function instead of real one
cs = conn.prepareCall("{ call ni_imsi_pkg.get_curx_wrapper(?,?) }");
// Pass the struct defined earlier
cs.setObject(1, rowStruct);
cs.registerOutParameter(2, OracleTypes.CURSOR);
// and other arguments for your real calll

如果你不能修改你的真实包,那么你可以为包装器创建一个新的,或者一个简单的过程;或者您甚至可以在匿名块中进行转换,但这会使 Java 代码更加复杂:

cs = (OracleCallableStatement) conn.prepareCall(
    "declare l_typ ni_imsi_rowtype; l_buf dual%rowtype; "
        + "begin l_typ := ?; l_buf.dummy := l_typ.dummy; ni_imsi_pkg.get_curx(l_buf, ?); "
        + "end;"
);

... 仍然绑定相同的结构,因此仍然需要 SQL 类型。只有语句发生了变化,但它现在可以在没有包装器的情况下调用原始过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-18
    • 2012-03-31
    • 2010-09-22
    • 1970-01-01
    • 2011-07-18
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多