Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve ResultSet using CallableStatement after executeBatch()

Tags:

java

jdbc

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

I've already tried to call callableStatement.getUpdateCount() and callableStatement.getMoreResults() before extracting ResultSet, but unsuccessfully.

like image 990
bsiamionau Avatar asked Feb 19 '26 02:02

bsiamionau


1 Answers

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.

You don't typically execute queries on batch because different queries would result in different shaped ResultSets (unless they were all for the same table with the same columns but a different query [but then why not just modify your query]).

like image 57
wmorrison365 Avatar answered Feb 21 '26 15:02

wmorrison365



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!