SQL Exception Handling is a mechanism that deals with runtime errors occurring during a database or SQL-related function. WebLogic maintains a log file named wl-domain in the admin domain directory under a directory called logs. Exceptions that occur while trying to create a connection pool, or MultiPool, and exceptions that occur on server startup, are logged in this file. However, during the course of your application development, many more errors will be occurring. These errors will have to be handled in order to ensure the smooth running of your application. Exceptions in the case of JDBC applications can be handled at two places, the first being the database end itself, and the second being the application end. It is good practice to handle exceptions at both ends. The programming industry's best practices for Java applications say that each code block should be enclosed in a try-catch-finally block. Ideally, the try block contains the code to be executed, the catch block contains the steps to be taken if an error condition occurs (for example, if it logs the specific error stack trace in a log file and rolls back any inconsistent data), and the finally block, in the case of JDBC applications, should close or release connections to the database, if possible. The Java API provides JDBC support classes, which contain a few classes supporting JDBC core functionalities. One of these is the java.sql.SQLexception class. This class, which extends the java.lang.Exception, helps to identify more information about the database SQL errors. Using this API, you can obtain the following information:
Note X/Open is a worldwide group of system vendors, independent software vendors, and major end users who have worked toward developing a vendor-independent, open operating environment based on certain default and international standards, which are called the X/Open portability guide or XPG. This group is striving to achieve database access standardization. These values are obtained in the catch block. Besides these, the methods of java.lang.Exception also can be used to get a list of exceptions. In a scenario where you may want the user to be notified of what the error is, but you do not wish to stop the application's progress, getNextException() can be used. This is similar to getting a stack trace of the errors occurring without stopping the progress of the application. The following code snippet shows how the previous methods are used: try{ ... //database related function like obtaining a connection to the //database, etc } catch(SQLException sqlEx){ while(!sqlEx==null){ System.out.println("The SQL Error Code is:" + sqlEx.getErrorCode()); System.out.println("The SQL Error Message is:" + sqlEx.getMessage()); System.out.println("The SQL State String is:" + sqlEx.getSQLState()); sqlEx= sqlEx.getNextException(); } } finally{ if(conn!=null){ try{ conn.close(); } catch(Exception e){ System.out.println("error occurring while attempting to close connection:"); System.out.println(e.getMessage()); System.out.println(e.printStackTrace()); } } } A few commonly occurring errors that may result in exceptions are:
|