简体   繁体   English

访问Oracle SYS_REFCURSOR

[英]Access Oracle SYS_REFCURSOR

I'm kinda stuck here. 我有点卡在这里。 I have a small stupid function in Oracle and try to access it with Java. 我在Oracle中有一个愚蠢的小功能,尝试用Java访问它。 The return value should be an SYS_REFCURSOR. 返回值应为SYS_REFCURSOR。 But I get an error if I try to call it in Java. 但是,如果尝试使用Java调用它,则会收到错误消息。

This is the Oracle function: 这是Oracle函数:

FUNCTION GET_TBL_AS_CUR
RETURN SYS_REFCURSOR
IS
  Tbl SYS_REFCURSOR;
BEGIN

  OPEN Tbl FOR SELECT * FROM test_tbl;
  RETURN Tbl;

END;

This is the Call in Java: 这是Java中的Call:

String stmt = "callStmt: {?= call dbschema.pkg_tbl_tools.get_tbl_as_cur()}";
callableStatement = conn.prepareCall(stmt);
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
System.out.println("Executing... " + stmt + "\n");

callableStatement.execute();

rs = ((OracleCallableStatement)callableStatement).getCursor(1);

} catch (Exception e) {
    System.out.println(e.getMessage());
}

The error message is: 错误消息是:

Unknown SQL-Type: sqlKind = UNINITIALIZED

What am I doing wrong?? 我究竟做错了什么??

Issue resolved. 问题解决了。

Thanks for your help. 谢谢你的帮助。 To summarize the final "trick" I'll post here the solution. 为了总结最后的“技巧”,我将在此处发布解决方案。

This style of call will work for any function or procedure in Oracle IF the function will return not an collection as ref cursor. 这种调用方式适用于Oracle IF中的任何函数或过程, 如果该函数将不返回集合作为ref游标,则不起作用。

String stmt = "{call ? := dbschema.pkg_tbl_tools.get_tbl_as_cur()}";

This style works in Oracle just fine, even if the return value is a ref cursor 即使返回值是ref游标,此样式在Oracle中也能正常工作

String stmt = "BEGIN ? := dbschema.pkg_tbl_tools.get_tbl_as_cur(); END;";

The following line 下一行

rs = ((OracleCallableStatement)callableStatement).getCursor(1);

could be changed into 可以变成

rs = (ResultSet) callableStatement.getObject(1);

Don't know why. 不知道为什么。 But it works now. 但是现在可以了。 Thanks again :) 再次感谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM