When querying database, exceptions might appear. JDBC provides getMessage() and getErrorCode() methods to handle those exceptions.:

Java Code:
   try
   {
       Statement s = conn.createStatement ();
       s.executeQuery ("XYZ"); // issue invalid query
       s.close ();
   }
   catch (SQLException e)
   {
       System.err.println ("Error message: " + e.getMessage ());
       System.err.println ("Error number: " + e.getErrorCode ());
   }