I made a generic wrapper to simplify JDBC use and I use something
similar. You could do something like this (haven't tested it):
for(
hasResultSet=statement.execute(); // Comment(1)
hasResultSet==true || (updateCount=statement.getUpdateCount())!=-1;
// Comment(2)
hasResultSet=statement.getMoreResults() // Comment(3)
){
if (hasResult) {
writer.write(statement.getResultSet()) ;
}else{
writer.write(updateCount);
}
}
Comments:
1. Execute the statement, returns true if first result is a resultset,
false if update count or no results
2. will enter loop if we have a result or we have an updatecount
3. we processed a result, try to get another one
FYI, in H2 *all* stored procedures return a ResultSet which is a problem
if you want to have a generic way to process statements. If a stored
procedure return only update counts, H2 returns a dummy rowset with 1
field named <procedure_name> and a value of (null). Was pulling my hair
for a while wondering why my framework worked everywhere except H2. Had
to change to Apache Derby for that reason.
On 3/23/2011 11:56 AM, dodtsair wrote:
I was expecting the wrong behavior from getMoreResults. I am writing
some code that will likely expect more then one result from some sql.
So I wanted to iterate over it.
something like while(iterator.hasNext()) {iterator.next}
So I wrote the following code:
while (!(statement.getMoreResults() == false&&
(updateCount = statement.getUpdateCount()) == -1)) {
ResultSet result = statement.getResultSet();
if (result != null) {
writer.write(result);
}
writer.write(updateCount);
}
The assumption being that I could have 0 - many results and I could
use getMoreResults to cover the 0 case. When the body of the loop
never executed I wrote the unit test to simplify the use case in an
attempt to find my error.
Thanks for the help.
On Mar 22, 10:33 pm, Thomas Mueller<[email protected]>
wrote:
Hi,
Right, but it doesn't explain why he gets "Select results:null".
It does. Statement.getMoreResults() is simply the wrong method.
The culprit seems to be the call to "statement.getMoreResults()"
Exactly. It's wrong to call Statement.execute() and then
Statement.getMoreResults(). All databases I have tested return
"false". The correct way is Statement.execute() and then
Statement.getResultSet().
Regards,
Thomas
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en.