1 package org.hibernate.exception; 3 4 import org.hibernate.JDBCException; 5 import org.hibernate.util.JDBCExceptionReporter; 6 7 import java.sql.SQLException ; 8 9 14 public final class JDBCExceptionHelper { 15 16 private JDBCExceptionHelper() { 17 } 18 19 28 public static JDBCException convert(SQLExceptionConverter converter, SQLException sqlException, String message) { 29 return convert( converter, sqlException, message, "???" ); 30 } 31 32 41 public static JDBCException convert(SQLExceptionConverter converter, SQLException sqlException, String message, String sql) { 42 JDBCExceptionReporter.logExceptions( sqlException, message + " [" + sql + "]" ); 43 return converter.convert( sqlException, message, sql ); 44 } 45 46 52 public static int extractErrorCode(SQLException sqlException) { 53 int errorCode = sqlException.getErrorCode(); 54 SQLException nested = sqlException.getNextException(); 55 while ( errorCode == 0 && nested != null ) { 56 errorCode = nested.getErrorCode(); 57 nested = nested.getNextException(); 58 } 59 return errorCode; 60 } 61 62 68 public static String extractSqlState(SQLException sqlException) { 69 String sqlState = sqlException.getSQLState(); 70 SQLException nested = sqlException.getNextException(); 71 while ( sqlState == null && nested != null ) { 72 sqlState = nested.getSQLState(); 73 nested = nested.getNextException(); 74 } 75 return sqlState; 76 } 77 78 84 public static String extractSqlStateClassCode(SQLException sqlException) { 85 String 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 |