java - Null resultsets when calling Sybase stored procedure through JDBC -
i'm calling sybase stored procedure returns multiple resultsets through jdbc. need specific result set has column named "result" 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 resultset , add elements returned 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 cs.getmoreresults
returns lot of null resultsets till reaches target one. can't use cs.getmoreresults
loop condition because returns false null resultsets.
i put fixed number end loop in condition wanted result set wasn't returned prevent going infinite loop. worked fine don't think right.
i think null resultsets returned assignment in sybase select @variable = value
has faced before?
you misinterpreting return value of getmoreresults()
. ignoring return value of execute()
, method returns boolean
indicating type of first result:
true
: resultresultset
false
: result update count
if result true
, use getresultset()
retrieve resultset
, otherwise getupdatecount()
retrieve update count. if update count -1
means there no more results. note update count -1
when current result resultset
. know getresultset()
should return null if there no more results or if result update count (this last condition why many null
values).
now if want retrieve more results, call getmoreresults()
(or brother accepting int
parameter). return value of boolean
has same meaning of execute()
, false
does not mean there no more results!
there no more results if getmoreresults()
returns false , getupdatecount()
returns -1
(as documented in javadoc)
essentially means if want correctly process results need below:
boolean result = stmt.execute(...); while(true) { if (result) { resultset rs = stmt.getresultset(); // resultset ... } else { int updatecount = stmt.getupdatecount(); if (updatecount == -1) { // no more results break; } // update count ... } result = stmt.getmoreresults(); }
my guess getting lot of update counts before actual resultset
.
i not familiar sybase, cousin sql server has 'annoying' feature return update counts stored procedures if don't explicitly put set nocount on;
@ start of stored procedure.
note: part of answer based on answer execute “sp_msforeachdb” in java application
Comments
Post a Comment