使用 JDBC 检索 Oracle 游标
我在尝试使一个简单的 Oracle 游标检索过程与 JDBC 一起工作时遇到了一些挫折。
我不断收到错误“[Oracle][ODBC][Ora]ORA-06553: PLS-306: 调用“GETNAME”时参数数量或类型错误”,但我无法弄清楚我做错了什么。
这是我的 Java 代码:
CallableStatement stmt = connection.prepareCall("call getName(?)");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
stmt.close();
con.close();
这是我在 Oracle 中的过程:
CREATE OR REPLACE PROCEDURE getName(cur out SYS_REFCURSOR)
IS
BEGIN
OPEN cur FOR
SELECT name FROM customer;
END;
错误发生在 stmt.execute()
上。
提前致谢。
顺便说一句,我正在使用 Oracle 10.2.0。
I have been experiencing some frustrations trying to make a simple Oracle cursor retrieval procedure work with JDBC.
I keep on getting an error of "[Oracle][ODBC][Ora]ORA-06553: PLS-306: wrong number or types of arguments in call to 'GETNAME'", but I cannot figure out what I am doing wrong.
Here is my code in Java:
CallableStatement stmt = connection.prepareCall("call getName(?)");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
stmt.close();
con.close();
Here is my procedure in Oracle:
CREATE OR REPLACE PROCEDURE getName(cur out SYS_REFCURSOR)
IS
BEGIN
OPEN cur FOR
SELECT name FROM customer;
END;
The error occurs on stmt.execute()
.
Thanks in advance.
By the way, I am working with Oracle 10.2.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我基本上尝试了同样的事情,它对我有用。唯一的区别是我使用的 Oracle JDBC 库没有方法
registerOutputParameter
;我改用registerOutParameter
。也许您正在调用通用 JDBC 方法,而不是支持 Oracle 类型的特定于 Oracle 的方法。我能想到的唯一其他解释是您的 Java 代码连接到错误的模式,并访问不同的
getName
对象。I tried essentially the same thing and it worked for me. The only difference was that the Oracle JDBC library I am using does not have a method
registerOutputParameter
; I usedregisterOutParameter
instead. Perhaps you are calling a generic JDBC method instead of the Oracle-specific one that support Oracle types.The only other explanation I can think of is that your Java code is connecting to the wrong schema, and accessing a different
getName
object.不,这是错误的。您不应该返回原始光标。您应该调用存储过程并迭代 ResultSet。
Nope, this is wrong. You should not be returning a raw cursor. You should call the stored proc and iterate through the ResultSet.