I’m calling a Sybase stored procedure that returns multiple resultsets through JDBC.
I need to get a specific result set that has a column named “Result”
This is my code :
CallableStatement cs = conn.prepareCall(sqlCall);
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
ResultSet rs=null;
int count = 1;
boolean flag = true;
while (count < 20000 && flag == true) {
cs.getMoreResults();
rs = cs.getResultSet();
if (rs != null) {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int columnsCount = resultSetMetaData.getColumnCount();
if (resultSetMetaData.getColumnName(1).equals("Result")) {
// action code resultset found
flag = false;
// loop on the resultset and add the elements returned to an array list
while (rs.next()) {
int x = 1;
while (x <= columnsCount) {
result.add(rs.getString(x));
x++;
}
}
result.add(0, cs.getString(1));
}
}
count++;
}
What happens here is that cs.getMoreResults returns a lot of null resultsets till it reaches the target one. I can’t use cs.getMoreResults as loop condition because it returns false for null resultsets.
I put a fixed number to end the loop in condition the wanted result set wasn’t returned to prevent it from going into infinite loop. It worked fine but I don’t think this is right.
I think the null resultsets returned from the assignment in Sybase select @variable = value
Has anyone faced this before?
You are misinterpreting the return value of
getMoreResults(). You are also ignoring the return value ofexecute(), this method returns abooleanindicating the type of the first result:true: result is aResultSetfalse: result is an update countIf the result is
true, then you usegetResultSet()to retrieve theResultSet, otherwisegetUpdateCount()to retrieve the update count. If the update count is-1it means there are no more results. Note that the update count will also be-1when the current result is aResultSet. It is also good to know thatgetResultSet()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 manynullvalues).Now if you want to retrieve more results, you call
getMoreResults()(or its brother accepting anintparameter). The return value ofbooleanhas the same meaning as that ofexecute(), sofalsedoes not mean there are no more results!There are only no more results if the
getMoreResults()returns false andgetUpdateCount()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:
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