Java SQLException extractSqlStateClassCode(SQLException sqlException)

Here you can find the source of extractSqlStateClassCode(SQLException sqlException)

Description

For the given SQLException, locates the X/Open-compliant SQLState's class code.

License

LGPL

Parameter

Parameter Description
sqlException The exception from which to extract the SQLState class code

Return

The SQLState class code, or null.

Declaration

public static String extractSqlStateClassCode(SQLException sqlException) 

Method Source Code


//package com.java2s;
/*//from w w  w  . j  a  v a  2 s .  co  m
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.sql.SQLException;

public class Main {
    /**
     * For the given SQLException, locates the X/Open-compliant SQLState's class code.
     *
     * @param sqlException The exception from which to extract the SQLState class code
     * @return The SQLState class code, or null.
     */
    public static String extractSqlStateClassCode(SQLException sqlException) {
        return determineSqlStateClassCode(extractSqlState(sqlException));
    }

    public static String determineSqlStateClassCode(String sqlState) {
        if (sqlState == null || sqlState.length() < 2) {
            return sqlState;
        }
        return sqlState.substring(0, 2);
    }

    /**
     * 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;
    }
}

Related

  1. exceptionMsg2LocalizedStr(final Throwable e)
  2. exceptionMsg2str(final Throwable e)
  3. exceptionToString(Throwable e)
  4. extractErrorCode(SQLException sqlException)
  5. extractNestedSQLExceptions( SQLException exception)
  6. getAllMessages(Throwable t, boolean includeExceptionName)
  7. getAllMessagesArray(Throwable t, boolean includeExceptionName)
  8. getAllSqlExceptionMessages(SQLException t)
  9. getAllSqlExceptionMessagesArray(SQLException t, boolean includeExceptionName)