package jef.database.exception; import java.sql.SQLException; public class JDBCExceptionHelper { /** * For the given SQLException, locates the vendor-specific error code. * * @param sqlException * The exception from which to extract the SQLState * @return The error code. */ public static int extractErrorCode(SQLException sqlException) { int errorCode = sqlException.getErrorCode(); SQLException nested = sqlException.getNextException(); while (errorCode == 0 && nested != null) { errorCode = nested.getErrorCode(); nested = nested.getNextException(); } return errorCode; } /** * For the given SQLException, locates the X/Open-compliant SQLState. * * @param sqlException * The exception from which to extract the SQLState * @return The SQLState code, or null. */ public static String extractSqlState(SQLException sqlException) { String sqlState = sqlException.getSQLState(); SQLException nested = sqlException.getNextException(); while (sqlState == null && nested != null) { sqlState = nested.getSQLState(); nested = nested.getNextException(); } return sqlState; } }