Ask Google

Custom Search

Sunday, January 23, 2011

Writing JDBC Applications with MySQL - Error Handling

If you want to trap errors, execute your JDBC operations within a try block and use an exception handler to display information about the cause of any problems that occur. JDBC provides getMessage() and getErrorCode() methods that may be invoked when an exception occurs to obtain the error message and the numeric error code. The following example deliberately issues a malformed query. When it runs, the executeQuery() method fails and raises an exception that is handled in the catch block:

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 ());
   }

No comments:

Post a Comment

5L15K

  © Blogger template The Beach by Ourblogtemplates.com 2009

Back to TOP