// message, SQLstate, and vendor code. System.out.println(”A SQLException was caught!”); System.out.println(”Message: ” + ex.getMessage()); System.out.println(”SQLState: ” + ex.getSQLState()); System.out.println(”Vendor Code: ” + ex.getErrorCode()); } An SQLWarning is similar to an SQLException (it extends SQLException). The main difference is in semantics. If an SQLException is thrown, it is considered to be a critical error (one that needs attention). If an SQLWarning is thrown, it is considered to be a non-critical error (a warning or informational message). For this reason, JDBC treats SQLWarnings much differently than SQLExceptions. SQLExceptions are thrown just like any other type of exception; SQLWarnings are not thrown, but put on a list of warnings on an owning object type (for instance, Connection, Statement, or ResultSet, which we ll cover later). Because they are put on a list, it is up to the application to poll for warnings after the completion of an operation. Listing 10.1 shows a method that accepts an SQLWarning and places it on a list. Listing 10.1 Placing an SQL Warning on a list. //———————————————————————- —- // setWarning // Sets the given SQLWarning in the warning chain. If null, the // chain is reset. The local attribute lastWarning is used // as the head of the chain. //————————————————————— —— — protected void setWarning( SQLWarning warning) { // A null warning can be used to clear the warning stack if (warning == null) { lastWarning = null; } else { // Set the head of the chain. We’ll use this to walk through the // chain to find the end. SQLWarning chain = lastWarning; // Find the end of the chain. When the current warning does // not have a next pointer, it must be the end of the chain. while (chain.getNextWarning() != null) { chain = chain.getNextWarning(); } // We’re at the end of the chain. Add the new warning chain.setNextWarning(warning); } } Listing 10.2 uses this method to create two SQLWarnings and chain them together. Listing 10.2 Chaining SQLWarnings together.
Are you tired of finding web hosting providers listed as inexpensive?Just check our Inexpensive Web Hosting services.