Java SQLException getSQLErrorCode(SQLException ex_)

Here you can find the source of getSQLErrorCode(SQLException ex_)

Description

Sometimes the SQL error code isn't set and we have to pull it from the message.

License

BSD License

Parameter

Parameter Description
ex_ the SQL exception

Return

the correct error code.

Declaration

static public int getSQLErrorCode(SQLException ex_) 

Method Source Code


//package com.java2s;
/*L/*w ww .  jav a 2 s.c om*/
 * Copyright ScenPro Inc, SAIC-F
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/cadsr-sentinal/LICENSE.txt for details.
 */

import java.sql.SQLException;

public class Main {
    /**
     * Sometimes the SQL error code isn't set and we have to pull it from the message.
     * 
     * @param ex_ the SQL exception
     * @return the correct error code.
     */
    static public int getSQLErrorCode(SQLException ex_) {
        int error = ex_.getErrorCode();
        if (error != 0)
            return error;

        String msg = ex_.toString();
        int pos = msg.indexOf("ORA-");
        if (pos < 0)
            return -9999;

        pos += 4;
        String[] temp = msg.substring(pos).split("[:, ]");
        if (temp.length < 2)
            return -9998;

        return Integer.valueOf(temp[0]);
    }
}

Related

  1. getExceptionCause(Throwable e)
  2. getExceptionMessage(Throwable t)
  3. getFullMessage(SQLException exception)
  4. getNextExceptionFromLastCause(Exception e)
  5. getSingleSQLExceptionCause(SQLException e)
  6. getSQLExceptionMessage(SQLException ex)
  7. getSqlStateString(SQLException se)
  8. getStringFromException(java.lang.Throwable exception)
  9. isConnectionError(final SQLException error)