Description
If there are multiple results or if the type or number of results returned by a Statement object are not known until run time, the Statement object should be executed with the method execute
. The methods getMoreResults
, getUpdateCount
, and getResultSet
can be used to retrieve all the results.
By default, each call to the method getMoreResults closes any previous ResultSet
object returned by the method getResultSet
. However, the method getMoreResults
may take a parameter that specifies whether a ResultSet
object returned by getResultSet
should be closed. The Statement interface defines three constants that can be supplied to the method getMoreResults:
- CLOSE_CURRENT_RESULT — indicates that the current ResultSet object should be closed when the next ResultSet object is returned
- KEEP_CURRENT_RESULT — indicates that the current ResultSet object should not be closed when the next ResultSet object is returned
- CLOSE_ALL_RESULTS — indicates that any ResultSet objects that have been kept open should be closed when the next result is returned
If the current result is an update count and not a ResultSet
object, any parameter passed to getMoreResults
is ignored.
To determine whether a driver implements this feature, an application can call the
DatabaseMetaData
method supportsMultipleOpenResults
.
An example:
Statement stmt = conn.createStatement();
boolean retval = cstmt.execute(sql_queries);
ResultSet rs;
int count;
do {
if (retval == false) {
count = stmt.getUpdateCount();
if (count == -1) {
// no more results
break;
} else {
// process update count
}
} else {
rs = stmt.getResultSet();
// process ResultSet
}
retval = stmt.getMoreResults();
while (true);