java接收rowtype类型_从Java访问存储过程的%ROWTYPE

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

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

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类型.只有语句更改,但它现在可以在没有包装器的情况下调用原始过程.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值