问题描述
我正在调用一个带有很少out参数的sp,根据我的要求,我需要在某些条件下使用ResultSet,而在其他条件下使用out参数.但是,使用executeQuery()时,我会得到JZ0R0:ResultSet已关闭错误.(我正在使用Sybase 15)
I am calling one sp with few out params and as per my requirement,I need to use ResultSet on some condition and out params on other conditions. But using executeQuery(), I am getting, JZ0R0: ResultSet has already been closed error.(I am using Sybase 15)
这里是示例:
Connection conn = ~~;
CallableStatement cstmt = conn.prepareCall("{call my_sp(?)");
cstmt.registerOutParameter(1,java.sql.Types.INTEGER);
ResultSet rs = cstmt.executeQuery();
如果我尝试这样做,请在下面的代码下面
If i try to do, below code now,
int res = cstmt.getInt(1);
然后尝试如下迭代ResultSet对象,然后得到以上ResultSet关闭错误.
Then try to iterate the ResultSet object, as below, then I get above ResultSet close error.
ResultSetMetaData rsmd = rs.getMetaData();
有什么方法可以获取输出参数值,然后在ResultSet对象上进行迭代,反之亦然?
Is there any way I can get the output param value and then iterate on ResultSet object or vice-versa?
推荐答案
根据JDBC规范,必须在输出参数之前检索结果集
According to the JDBC spec you must retrieve the result set before output parameters
这是原始规格
7.1.7在结果后检索OUT参数
7.1.7 Retrieve OUT Parameters after Results
由于某些DBMS的局限性,建议为了最大的可移植性,应在检索OUT参数之前先检索通过执行CallableStatement对象生成的ResultSet对象中的所有结果.从结果集中检索所有值后,方法ResultSet.next将返回false.
Because of limitations imposed by some DBMSs, it is recommended that for maximum portability, all of the results in a ResultSet object generated by the execution of a CallableStatement object should be retrieved before OUT parameters are retrieved. When all values have been retrieved from a result set, the method ResultSet.next will return false.
如果CallableStatement对象返回多个ResultSet对象(只有在调用方法execute的情况下才可能执行),则应在检索OUT参数之前先检索所有结果.在这种情况下,为确保已访问所有结果,需要调用Statement方法getResultSet,getUpdateCount和getMoreResults,直到没有更多结果为止.用尽所有结果后,方法getMoreResults返回false,方法getUpdateCount返回-1.
If a CallableStatement object returns multiple ResultSet objects (which is possible only if it is executed with a call to the method execute), all of the results should be retrieved before OUT parameters are retrieved. In this case, to be sure that all results have been accessed, the Statement methods getResultSet, getUpdateCount, and getMoreResults need to be called until there are no more results. When all results have been exhausted, the method getMoreResults returns false, and the method getUpdateCount returns -1.
这篇关于JDBC executeQuery()返回ResultSet和输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!