You are misinterpreting the return value of getMoreResults()
. You are also ignoring the return value of execute()
, this method returns a boolean
indicating the type of the first result:
true
: result is a ResultSet
false
: result is an update count
If the result is true
, then you use getResultSet()
to retrieve the ResultSet
, otherwise getUpdateCount()
to retrieve the update count. If the update count is -1
it means there are no more results. Note that the update count will also be -1
when the current result is a ResultSet
. It is also good to know that getResultSet()
should return null if there are no more results or if the result is an update count (this last condition is why you get so many null
values).
Now if you want to retrieve more results, you call getMoreResults()
(or its brother accepting an int
parameter). The return value of boolean
has the same meaning as that of execute()
, so false
does not mean there are no more results!
There are only no more results if the getMoreResults()
returns false and getUpdateCount()
returns -1
(as also documented in the Javadoc)
Essentially this means that if you want to correctly process all results you need to do something like below:
boolean result = stmt.execute(...);
while(true) {
if (result) {
ResultSet rs = stmt.getResultSet();
// Do something with resultset ...
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
// Do something with update count ...
}
result = stmt.getMoreResults();
}
My guess is that you are getting a lot of update counts before you get the actual ResultSet
.
I am not really familiar with Sybase, but its cousin SQL Server has the 'annoying' feature to return update counts from stored procedures if you don't explicitly put SET NOCOUNT ON;
at the start of the stored procedure.
NOTE: Part of this answer is based on my answer to Execute “sp_msforeachdb” in a Java application
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…