Presented below is an example that shows how to handle SQL errors.
Java Code:
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();
      System.out.println("");
      }
    }
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
      }
}