Detect SQL errors or warningsTag(s): JDBC


When the database server executes an SQL statement successfully, but encounters a warning condition, it sets the SQLSTATE class field to "01". The subclass code then indicates the cause of the warning. This warning can be an ANSI or X/Open warning message (if supported by the driver) the subclass code in the range "000" to "006". Also the driver can provide is own warning codes in the "01" class.

Class Subclass
01    000        Success with warning

01    002        Disconnect error-transaction rolled back

01    003        Null value eliminated in set function

01    004        String data, right truncation

01    005        Insufficient item descriptor areas

01    006        Privilege not revoked

01    007        PRIVILEGE NOT GRANTED

The JDK API doc reveals that "The SQLWarning class provides information on a database access warnings. Warnings are silently chained to the object whose method caused it to be reported". So after DB operation, you ask the Connection object if there are any warnings. Depending on the vendor driver, informations (not only Warnings) can be found in the SQLWarning object.

       dbConn = DriverManager.getConnection
       SQLWarning w = dbConn.getWarnings();
       // If a SQLWarning object was returned by the
       // Connection object, the warning messages are displayed.
       // You may have multiple warnings chained together
       if (w != null) {
         System.out.println ("\n *** SQL Warning ***\n");
         while (w != null) {
           System.out.println ("Message:  " + w.getMessage ());
           System.out.println ("SQLState: " + w.getSQLState ());
           System.out.println ("VendorCode:   " + w.getErrorCode ());
           System.out.println ("");
           w = w.getNextWarning ();

When an error occurs, a SQLException is thrown. You can interrogate the SQLException object to know more details about the actual error. Note that SQLException can be chained. The SQLException object contains :

  1. A string describing the error. This is used as the Java Exception message, available via the getMessage method.
  2. A "SQLState" string, which follows the XOPEN SQLState conventions.
  3. An integer error code that is specific to each vendor.
try {
    //   ... some SQL operations
catch (SQLException ex) {
    // SQLException occured.
    // There could be multiple error objects chained together
    System.out.out.println ("*** SQLException caught ***");

    while (ex != null) {
      System.out.println ("SQLState: " + ex.getSQLState () + "");
      System.out.println ("Message: " + ex.getMessage() + "");
      System.out.println ("Vendor ErrorCode: " + ex.getErrorCode() + "");
      ex = ex.getNextException();
catch (java.lang.Exception ex) {
    // Some other type of exception occurred
    System.out.println("*** Exception caught ***");
    System.out.println(ex.getMessage()+ "");
finally {
    // Close the database connection.
    try {
      if (con != null) con.close();
    catch (SQLException ignored) {
      //do nothing
Check your DBMS manual for a full list of supported SQLState.
blog comments powered by Disqus