My app uses MySQL on one platform and SQLite on another, as such there are differences, such as that when using query like DELETE FROM USERS:
- On MySQL,
PreparedStatement.getResultSet()will returnnull. - On SQLite,
PreparedStatement.getResultSet()will throwjava.sql.SQLException: no ResultSet available.
This may or may not be a bug in SQLite implementation (I think it is supposed to return null), but I have to deal with this somehow.
I could use a try { ... } catch (SQLException e) { ... } and if the exception message is "no ResultSet available", simply return null manually. This doesn’t feel like a right way to do it though.
I could put up an if that makes a check on what JDBC driver is being used and react accordingly, but again, that doesn’t feel like a good solution to the problem.
What I would really like is either a method like .hasResultSet() that returns a boolean OR a way to get the SQL command (SELECT, UPDATE, INSERT etc) from a statement that has been executed. I can find neither of the two in SQL API though.
When executing a query that returns an unknown amount of results, then you need to use
execute(). This method returns abooleanindicating the type of 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, so the behavior of SQL Lite to throw an exception seems to be wrong.Now if you want to retrieve more results, you call
getMoreResults()(or its brother accepting anintparameter). Thebooleanreturn value of this method has 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: