KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > exception > JDBCExceptionHelper


1 // $Id: JDBCExceptionHelper.java,v 1.3 2005/05/05 14:32:19 steveebersole Exp $
2
package org.hibernate.exception;
3
4 import org.hibernate.JDBCException;
5 import org.hibernate.util.JDBCExceptionReporter;
6
7 import java.sql.SQLException JavaDoc;
8
9 /**
10  * Implementation of JDBCExceptionHelper.
11  *
12  * @author Steve Ebersole
13  */

14 public final class JDBCExceptionHelper {
15
16     private JDBCExceptionHelper() {
17     }
18
19     /**
20      * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
21      * appropriate logging.
22      *
23      * @param converter The converter to use.
24      * @param sqlException The exception to convert.
25      * @param message An optional error message.
26      * @return The converted JDBCException.
27      */

28     public static JDBCException convert(SQLExceptionConverter converter, SQLException JavaDoc sqlException, String JavaDoc message) {
29         return convert( converter, sqlException, message, "???" );
30     }
31
32     /**
33      * Converts the given SQLException into Hibernate's JDBCException hierarchy, as well as performing
34      * appropriate logging.
35      *
36      * @param converter The converter to use.
37      * @param sqlException The exception to convert.
38      * @param message An optional error message.
39      * @return The converted JDBCException.
40      */

41     public static JDBCException convert(SQLExceptionConverter converter, SQLException JavaDoc sqlException, String JavaDoc message, String JavaDoc sql) {
42         JDBCExceptionReporter.logExceptions( sqlException, message + " [" + sql + "]" );
43         return converter.convert( sqlException, message, sql );
44     }
45
46     /**
47      * For the given SQLException, locates the vendor-specific error code.
48      *
49      * @param sqlException The exception from which to extract the SQLState
50      * @return The error code.
51      */

52     public static int extractErrorCode(SQLException JavaDoc sqlException) {
53         int errorCode = sqlException.getErrorCode();
54         SQLException JavaDoc nested = sqlException.getNextException();
55         while ( errorCode == 0 && nested != null ) {
56             errorCode = nested.getErrorCode();
57             nested = nested.getNextException();
58         }
59         return errorCode;
60     }
61
62     /**
63      * For the given SQLException, locates the X/Open-compliant SQLState.
64      *
65      * @param sqlException The exception from which to extract the SQLState
66      * @return The SQLState code, or null.
67      */

68     public static String JavaDoc extractSqlState(SQLException JavaDoc sqlException) {
69         String JavaDoc sqlState = sqlException.getSQLState();
70         SQLException JavaDoc nested = sqlException.getNextException();
71         while ( sqlState == null && nested != null ) {
72             sqlState = nested.getSQLState();
73             nested = nested.getNextException();
74         }
75         return sqlState;
76     }
77
78     /**
79      * For the given SQLException, locates the X/Open-compliant SQLState's class code.
80      *
81      * @param sqlException The exception from which to extract the SQLState class code
82      * @return The SQLState class code, or null.
83      */

84     public static String JavaDoc extractSqlStateClassCode(SQLException JavaDoc sqlException) {
85         String JavaDoc sqlState = extractSqlState( sqlException );
86
87         if ( sqlState == null || sqlState.length() < 2 ) {
88             return sqlState;
89         }
90
91         return sqlState.substring( 0, 2 );
92     }
93
94 }
95
Popular Tags