Java Reference
In-Depth Information
Example 17−1: ExecuteSQL.java (continued)
ResultSet rs = s.getResultSet(); // Get results
printResultsTable(rs, System.out); // Display them
}
else {
// If the SQL command that was executed was some
// kind of update rather than a query, then it
// doesn't return a ResultSet. Instead, we just
// print the number of rows that were affected.
int numUpdates = s.getUpdateCount();
System.out.println("Ok. " + numUpdates +
" rows affected.");
}
// Now go see if there are even more results, and
// continue the results display loop if there are.
status = s.getMoreResults();
} while(status || s.getUpdateCount() != -1);
}
// If a SQLException is thrown, display an error message.
// Note that SQLExceptions can have a general message and a
// DB-specific message returned by getSQLState()
catch (SQLException e) {
System.err.println("SQLException: " + e.getMessage()+ ":" +
e.getSQLState());
}
// Each time through this loop, check to see if there were any
// warnings. Note that there can be a whole chain of warnings.
finally { // print out any warnings that occurred
SQLWarning w;
for(w=conn.getWarnings(); w != null; w=w.getNextWarning())
System.err.println("WARNING: " + w.getMessage() +
":" + w.getSQLState());
}
}
}
// Handle exceptions that occur during argument parsing, database
// connection setup, etc. For SQLExceptions, print the details.
catch (Exception e) {
System.err.println(e);
if (e instanceof SQLException)
System.err.println("SQL State: " +
((SQLException)e).getSQLState());
System.err.println("Usage: java ExecuteSQL [-d <driver>] " +
"[-u <user>] [-p <password>] <database URL>");
}
// Be sure to always close the database connection when we exit,
// whether we exit because the user types 'quit' or because of an
// exception thrown while setting things up. Closing this connection
// also implicitly closes any open statements and result sets
// associated with it.
finally {
try { conn.close(); } catch (Exception e) {}
}
}
/**
* This method attempts to output the contents of a ResultSet in a
Search WWH ::




Custom Search