gpt4 book ai didi

java - CallableStatement getResultSet 使用输出参数时返回 null

转载 作者:行者123 更新时间:2023-12-01 13:52:33 26 4
gpt4 key购买 nike

当我尝试从存储过程中检索多个结果集时,我得到了一些奇怪的结果。如果我直接从 SQL Server Management Studio 执行,存储过程可以正常工作。

myProc= conn.prepareCall("{? = call abc.foo(?, ?, ?, ?, ?, ?, ?, ?, ?)}");   
myProc.registerOutParameter(1, java.sql.Types.INTEGER);
myProc.setString(2, lookup.getEmailAddress());
//...other parameters
boolean isResultSet = myProc.execute(); //returns true
dbReturnVal = myProc.getInt(1); //correctly retrieves return code
ResultSet rs = myProc.getResultSet(); //returns NULL
while (rs.next()) { //exception

由于 executegetResultSet 的 javadoc,这很令人困惑:

execute: Returns true if the first result is a ResultSet object; false if the first result is an update count or there is no result

getResultSet: the current result as a ResultSet object or null if the result is an update count or there are no more results

如果没有更多结果,getResultSet 应该只返回 null,对吗?但是,我从在 SQL Server 中执行查询得知返回了 3 个结果集。我在这里遗漏了什么吗?

作为测试,我运行了 getMoreResults() 来查看结果集是否由于某种原因丢失:

 dbReturnVal = myProc.getInt(1); //correctly retrieves return code
if(myProc.getMoreResults())
{
ResultSet rs = myProc.getResultSet(); //STILL returns null
while (rs.next()) { //exception
...

getMoreResults 返回 true,但是当我尝试获取结果集时,它仍然返回 NULL。无论如何,这并不是我想要的,因为它会跳过我的结果集,但我至少应该检索第二个结果集。

什么可能导致这种情况? SQL Server 返回结果集的方式是否导致了问题,或者是否有我遗漏的内容?如有任何建议,我们将不胜感激。

更新

原来这和我创建的输出参数有关

myProc= conn.prepareCall("{? = call abc.foo(?, ?, ?, ?, ?, ?, ?, ?, ?)}");   
myProc.registerOutParameter(1, java.sql.Types.INTEGER);

我删除了参数并将调用更新为

myProc= conn.prepareCall("{call abc.foo(?, ?, ?, ?, ?, ?, ?, ?, ?)}");   
//myProc.registerOutParameter(1, java.sql.Types.INTEGER);
//moved up my parameters by 1

而且它成功了。我现在正确地得到了所有 3 个结果集。由于我不太关心返回代码,所以这可能没问题。从学术角度来看,为什么输出参数会导致检索结果集出现问题?

最佳答案

我知道这是一个较旧的问题,但想确认 API 支持同一调用中的返回值和结果集。您只需确保在处理结果集后读取 OUT 参数/返回值即可。

http://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html

For maximum portability, a call's ResultSet objects and update counts should be processed prior to getting the values of output parameters.

关于java - CallableStatement getResultSet 使用输出参数时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19861942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com