正确的代码来处理
ResultSetJDBC语句返回的多个:
PreparedStatement stmt = ...;boolean isResultSet = stmt.execute();int count = 0;while(true) { if(isResultSet) { rs = stmt.getResultSet(); while(rs.next()) { processEachRow(rs); } rs.close(); } else { if(stmt.getUpdateCount() == -1) { break; } log.info("Result {} is just a count: {}", count, stmt.getUpdateCount()); } count ++; isResultSet = stmt.getMoreResults();}重要位:
getMoreResults()
并execute()
返回false
以表明语句的结果只是一个数字,而不是一个ResultSet
。- 您需要检查
stmt.getUpdateCount() == -1
以了解是否还有更多结果。 - 确保关闭结果集或使用
stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT)



