带有 CallableStatement.getResultSet() 的 NullPointerException
我在 SQL Server 2005 中有一个存储过程,如下所示(简化版)
CREATE PROCEDURE FOO
@PARAMS
AS
BEGIN
-- STEP 1: POPULATE tmp_table
DECLARE @tmp_table TABLE (...)
INSERT INTO @tmp_table
SELECT * FROM BAR
-- STEP 2: USE @tmp_table FOR FINAL SELECT
SELECT abc, pqr
FROM BAZ JOIN @tmp_table
ON some_criteria
END
当我从 SQL Server Management Studio 运行此过程时,一切正常。但是,当我从 Java 程序调用相同的过程时,使用类似以下内容:
cs = connection.prepareCall("exec proc ?,");
cs.setParam(...);
rs = cs.getResultSet(); // BOOM - Null!
while(rs.next()) {...} // NPE!
我无法理解为什么返回的第一个结果集是 NULL。有人可以向我解释一下吗?
作为解决方法,如果我检查 cs.getMoreResults() 并且如果为 true,请尝试另一个 getResultSet() - 这次它返回正确的结果集。
请问有什么指点吗? (我正在使用 JTDS 驱动程序,如果重要的话)
谢谢, 拉吉
I have a stored proc in SQL Server 2005, which looks like the following (simplified)
CREATE PROCEDURE FOO
@PARAMS
AS
BEGIN
-- STEP 1: POPULATE tmp_table
DECLARE @tmp_table TABLE (...)
INSERT INTO @tmp_table
SELECT * FROM BAR
-- STEP 2: USE @tmp_table FOR FINAL SELECT
SELECT abc, pqr
FROM BAZ JOIN @tmp_table
ON some_criteria
END
When I run this proc from SQL Server Management Studio, things work fine. However, when I call the same proc from a Java program, using something like:
cs = connection.prepareCall("exec proc ?,");
cs.setParam(...);
rs = cs.getResultSet(); // BOOM - Null!
while(rs.next()) {...} // NPE!
I fail to understand why the first result set returned is NULL. Can someone explain this to me?
As a workaround, if I check cs.getMoreResults() and if true, try another getResultSet() - THIS time it returns the proper result set.
Any pointers please? (I'm using JTDS drivers, if it matters)
Thanks,
Raj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getResultSet( )
表示它返回 null“...如果结果是更新计数或没有更多结果”。看起来您的存储过程将具有更新计数和结果集,并且 getResultSet() 方法(可以说)只是执行 API 合约规定的操作。您可以尝试先检索更新计数。否则,请坚持使用您的“解决方法”。
The Javadoc for
getResultSet()
says that it returns null "... if the result is an update count or there are no more results". It looks like your stored procedure would have an update count and a resultset, and that thegetResultSet()
method is (arguably) just doing what the API contract says it should do.You could try retrieving the update count first. Otherwise, stick with your "workaround".
我想在选择正确答案后发布答案是毫无意义的。
我建议的解决方案是
在插入语句之前和
之后调用。否则,插入将返回结果集。
Kind of pointless posting an answer after the correct answer has been selected I guess.
The solution I suggest is calling
before the insert statement and
afterwards. Inserts return a resultset otherwise.