在executeBatch()之后使用CallableStatement检索ResultSet [英] Retrieve ResultSet using CallableStatement after executeBatch()
问题描述
我需要多次调用存储过程并为此使用 executeBatch()
。每次调用都应返回包含结果的表,但我无法访问此结果。下一个代码工作正常:
I need to call stored procedure multiple times and use executeBatch()
for this. Every call should return table with results, but I could not access this results. Next code works fine:
callableStatement.setString(1, "foo");
callableStatement.setString(2, "bar");
callableStatement.execute();
resultSet = callableStatement.getResultSet();
但是下一个代码没有按预期工作:
But next code doesn't work as expected:
for (String str : strings) {
callableStatement.setString(1, str);
callableStatement.setString(2, "bar");
callableStatement.addBatch();
}
callableStatement.executeBatch();
resultSet = callableStatement.getResultSet(); // returns null
我已经尝试调用 callableStatement.getUpdateCount( )
和 callableStatement.getMoreResults()
在提取ResultSet之前,但没有成功。
I've already tried to call callableStatement.getUpdateCount()
and callableStatement.getMoreResults()
before extracting ResultSet, but unsuccessfully.
推荐答案
这不是正确使用 #executeBatch
。批处理方法用于执行数据操作,而不是获取结果数据。也就是说,您可以执行批量插入/更新/删除但不能读取。 #executeBatch
的结果是更新计数,表示每次批处理操作进行了多少更改
This isn't really the correct use of #executeBatch
. The batch methods are for doing data manipulation rather that obtaining result data. That is, you can do batch inserts/ updates/ deletes but not reads. The result of #executeBatch
is the update-count, indicating how many changes were made per batched operation
意图是的,您可以通过减少网络开销和数据库往返来提高性能,以实现类似的数据操作操作。
The intention is that you can improve performance by alleviating network overhead and database round-trip for alike data manipulation operations.
您通常不会批量执行查询,因为会产生不同的查询在不同形状的 ResultSet
中(除非它们都是针对具有相同列但具有不同查询的同一个表[但为什么不只是修改您的查询])。
You don't typically execute queries on batch because different queries would result in different shaped ResultSet
s (unless they were all for the same table with the same columns but a different query [but then why not just modify your query]).
这篇关于在executeBatch()之后使用CallableStatement检索ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!