View RSS Feed

Java Database

Retrieving Exceptions

Rate this Entry
by , 03-10-2012 at 07:58 AM (892 Views)
The given code outputs the following that is contained in the SQLException or other exception which have been chained to it:
  • SQL State
  • Error description
  • Error Code
  • Cause

Java Code:
public static void
    printSQLException(SQLException ex) {
    for (Throwable e : ex) {
        if (e instanceof SQLException) {
            if (ignoreSQLException(
                ((SQLException)e).
                getSQLState()) == false)
                {
                e.printStackTrace(System.err);
                System.err.println("SQLState: " +
                    ((SQLException)e).getSQLState());
                System.err.println("Error Code: " +
                    ((SQLException)e).getErrorCode());
                System.err.println("Message: " +
                    e.getMessage());
                Throwable t = ex.getCause();
                while(t != null) {
                    System.out.println(
                        "Cause: " + t);
                    t = t.getCause();
                }
            }
        }
    }
}

Submit "Retrieving Exceptions" to Facebook Submit "Retrieving Exceptions" to Digg Submit "Retrieving Exceptions" to del.icio.us Submit "Retrieving Exceptions" to StumbleUpon Submit "Retrieving Exceptions" to Google

Categories
JDBC

Comments