Example usage for java.sql SQLException getSQLState

List of usage examples for java.sql SQLException getSQLState

Introduction

In this page you can find the example usage for java.sql SQLException getSQLState.

Prototype

public String getSQLState() 

Source Link

Document

Retrieves the SQLState for this SQLException object.

Usage

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

/**
 * Apply the current query timeout, if any, to the current <code>PreparedStatement</code>.
 * /*from w ww  . j av a  2  s  .  c om*/
 * @param pstm PreparedStatement
 * @see java.sql.PreparedStatement#setQueryTimeout
 */
public void applyQueryTimeout(PreparedStatement pstm) {
    if (hasQueryTimeout()) {
        try {
            pstm.setQueryTimeout(getQueryTimeoutInSeconds());
        } catch (SQLException e) {
            throw new JDBCException("failed to setQueryTimeout to :" + getQueryTimeoutInSeconds(), e,
                    e.getSQLState());
        }
    }
}

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

/**
 * Apply the current query timeout, if any, to the current <code>Statement</code>.
 * /*from   w  w  w  .jav a  2 s.c om*/
 * @param stmt Statement
 * @see java.sql.Statement#setQueryTimeout
 */
public void applyQueryTimeout(Statement stmt) {
    if (hasQueryTimeout()) {
        try {
            stmt.setQueryTimeout(getQueryTimeoutInSeconds());
        } catch (SQLException e) {
            throw new JDBCException("failed to setQueryTimeout to :" + getQueryTimeoutInSeconds(), e,
                    e.getSQLState());
        }
    }
}

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

/**
 * Apply the current query timeout, if any, to the current <code>java.sql.CallableStatement</code>.
 * //  w w  w.  ja v a  2s  . c om
 * @param cs java.sql.CallableStatement
 * @see java.sql.CallableStatement#setQueryTimeout
 */
public void applyQueryTimeout(java.sql.CallableStatement cs) {
    if (hasQueryTimeout()) {
        try {
            cs.setQueryTimeout(getQueryTimeoutInSeconds());
        } catch (SQLException e) {
            throw new JDBCException("failed to setQueryTimeout to :" + getQueryTimeoutInSeconds(), e,
                    e.getSQLState());
        }
    }
}

From source file:org.finra.dm.service.helper.DmErrorInformationExceptionHandler.java

/**
 * Returns {@code true} if the given throwable is a data truncation exception. This method does not check the causes of the given throwable.
 * <p/>//from   w  w  w  . j  a v a  2  s .c  om
 * This method will check the status codes and error codes of the underlying {@link SQLException}.
 *
 * @param throwable - throwable to check
 *
 * @return {@code true} if error is data truncation error, {@code false} otherwise.
 */
private boolean isDataTruncationException(Throwable throwable) {
    boolean isDataTruncationException = false;
    // Exception must be a SQLException
    if (throwable instanceof SQLException) {
        SQLException sqlException = (SQLException) throwable;

        if (sqlException instanceof DataTruncation) {
            // Some drivers throw nice data truncation errors (e.g. MySQL).
            isDataTruncationException = true;
        } else {
            // If drivers don't throw nice errors, we need to examine error codes.

            // Check SQL state first to see what kind of error it is.
            switch (sqlException.getSQLState()) {
            // Oracle depends on error codes.
            case ORACLE_SQL_STATE_CODE_ERROR:
                switch (sqlException.getErrorCode()) {
                // Oracle throws different error codes depending on whether the length was <= 4000 or not
                case ORACLE_DATA_TOO_LARGE_ERROR_CODE:
                case ORACLE_LONG_DATA_IN_LONG_COLUMN_ERROR_CODE:
                    isDataTruncationException = true;
                    break;

                // In all other cases, assume it is not a data truncation exception.
                default:
                    isDataTruncationException = false;
                    break;
                }
                break;

            // Postgres does not use error codes.
            case POSTGRES_SQL_STATE_CODE_TRUNCATION_ERROR:
                isDataTruncationException = true;
                break;

            // In all other cases, assume it is not a data truncation exception.
            default:
                isDataTruncationException = false;
                break;
            }
        }
    }
    return isDataTruncationException;
}

From source file:io.bibleget.BibleGetDB.java

public boolean connect() {
    try {//from  w ww  .jav  a  2s  .  c om
        instance.conn = DriverManager.getConnection("jdbc:derby:BIBLEGET", "bibleget", "bibleget");
    } catch (SQLException ex) {
        if (ex.getSQLState().equals("X0Y32")) {
            Logger.getLogger(BibleGetDB.class.getName()).log(Level.INFO, null,
                    "Table OPTIONS or Table METADATA already exists.  No need to recreate");
            return true;
        } else if (ex.getNextException().getErrorCode() == 45000) {
            //this means we already have a connection, so this is good too
            return true;
        } else {
            //Logger.getLogger(BibleGetDB.class.getName()).log(Level.SEVERE, null, ex.getMessage() + " : " + Arrays.toString(ex.getStackTrace()));
            Logger.getLogger(BibleGetDB.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    return true;
}

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

/**
 * @param bsql/*from  w ww  . ja v  a 2s.co m*/
 * @param returnType
 */
protected Object findCell00(BindedCompiledSQL bsql, SQLDataType returnType) {
    ObjectMapping m = bsql.getCompiledSQLToRun().getMapping();
    String rawSQL = bsql.getSQLToRun();
    if (m == null) {
        throw new ORMException("ObjectMapping is null. sql is:" + rawSQL);
    }

    Dialect dialect = m.getDbGroup().getDialect();

    //?
    LockMode lock = bsql.getLockMode();

    if (lock == LockMode.UPGRADE) {
        rawSQL = dialect.getForUpdateString(rawSQL);
    } else if (lock == LockMode.UPGRADE_NOWAIT) {
        rawSQL = dialect.getForUpdateNoWaitString(rawSQL);
    }

    RowDataLoader loader = bsql.getRowDataLoader();
    DBGroup db = m.getDbGroup();

    boolean measureTime = this.debugService.isMeasureTime();
    long startTime = 0L;
    if (measureTime) {
        startTime = System.nanoTime();
    }

    PreparedStatement pstm = null;
    ResultSet rs = null;

    try {
        Connection conn = getConnection(db, bsql.getTableCondition());
        pstm = conn.prepareStatement(rawSQL);
        this.applyQueryTimeout(pstm);
        bsql.prepareNamedParams(db.getDialect(), pstm);

        rs = pstm.executeQuery();

        if (this.debugService.isLogSQL()) {
            long timeCost = 0;
            if (measureTime) {
                timeCost = System.nanoTime() - startTime;
            }

            this.debugService.logSQL(bsql, rawSQL, timeCost);
        }

        if (rs.next()) {
            if (loader != null) {
                return loader.rs2Object(m, rs);
            } else if (returnType != null) {
                return returnType.getSQLValue(rs, 1);
            } else {
                return rs.getObject(1);
            }
        } else {
            if (bsql.isExceptionOnNoRecordFound()) {
                throw new DaoException("record not found for the query:[" + rawSQL + "], params:["
                        + bsql.getBindedParams() + "].");
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new JDBCException("Error Code:" + e.getErrorCode() + ", sql:" + rawSQL, e, e.getSQLState());
    } finally {
        CloseUtil.close(rs);
        CloseUtil.close(pstm);
    }
}

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

public Object findCell00(BindedCompiledSQL bsql, String returnType) {
    ObjectMapping m = bsql.getCompiledSQLToRun().getMapping();
    String rawSQL = bsql.getSQLToRun();
    if (m == null) {
        throw new ORMException("ObjectMapping is null. sql is:" + rawSQL);
    }//from ww  w  . j a  va2 s  .c o  m

    Dialect dialect = m.getDbGroup().getDialect();

    //?
    LockMode lock = bsql.getLockMode();

    if (lock == LockMode.UPGRADE) {
        rawSQL = dialect.getForUpdateString(rawSQL);
    } else if (lock == LockMode.UPGRADE_NOWAIT) {
        rawSQL = dialect.getForUpdateNoWaitString(rawSQL);
    }

    RowDataLoader loader = bsql.getRowDataLoader();
    DBGroup db = m.getDbGroup();

    boolean measureTime = this.debugService.isMeasureTime();
    long startTime = 0L;
    if (measureTime) {
        startTime = System.nanoTime();
    }

    PreparedStatement pstm = null;
    ResultSet rs = null;

    try {
        Connection conn = getConnection(db, bsql.getTableCondition());
        pstm = conn.prepareStatement(rawSQL);
        this.applyQueryTimeout(pstm);
        bsql.prepareNamedParams(db.getDialect(), pstm);

        rs = pstm.executeQuery();

        if (this.debugService.isLogSQL()) {
            long timeCost = 0;
            if (measureTime) {
                timeCost = System.nanoTime() - startTime;
            }

            this.debugService.logSQL(bsql, rawSQL, timeCost);
        }

        if (rs.next()) {
            if (loader != null) {
                return loader.rs2Object(m, rs);
            } else if (returnType != null) {
                SQLDataType type = db.getDialect().getDataType(returnType);

                return type.getSQLValue(rs, 1);
            } else {
                return rs.getObject(1);
            }
        } else {
            if (bsql.isExceptionOnNoRecordFound()) {
                throw new DaoException("record not found for the query:[" + rawSQL + "], params:["
                        + bsql.getBindedParams() + "].");
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new JDBCException("Error Code:" + e.getErrorCode() + ", sql:" + rawSQL, e, e.getSQLState());
    } finally {
        CloseUtil.close(rs);
        CloseUtil.close(pstm);
    }
}

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

public Object findObject(BindedCompiledSQL bsql) {
    ObjectMapping m = bsql.getCompiledSQLToRun().getMapping();
    String rawSQL = bsql.getSQLToRun();

    if (m == null) {
        throw new ORMException("ObjectMapping is null. sql is:" + rawSQL);
    }//from   www .j  a  va2s  .  c o  m

    RowDataLoader loader = bsql.getRowDataLoader();

    DBGroup db = m.getDbGroup();
    Dialect dialect = db.getDialect();

    //?
    LockMode lock = bsql.getLockMode();

    if (lock == LockMode.UPGRADE) {
        rawSQL = dialect.getForUpdateString(rawSQL);
    } else if (lock == LockMode.UPGRADE_NOWAIT) {
        rawSQL = dialect.getForUpdateNoWaitString(rawSQL);
    }

    //TODO: check if the defaultDialect supports prepared bind in limit clause, and put the limit to compiledSQL
    rawSQL = db.getDialect().getLimitedString(rawSQL, 0, 1);

    boolean measureTime = this.debugService.isMeasureTime();
    long startTime = 0L;
    if (measureTime) {
        startTime = System.nanoTime();
    }

    PreparedStatement pstm = null;
    ResultSet rs = null;

    try {
        Connection conn = getConnection(db, bsql.getTableCondition());
        pstm = conn.prepareStatement(rawSQL);
        this.applyQueryTimeout(pstm);

        bsql.prepareNamedParams(db.getDialect(), pstm);

        rs = pstm.executeQuery();

        if (this.debugService.isLogSQL()) {
            long timeCost = 0;
            if (measureTime) {
                timeCost = System.nanoTime() - startTime;
            }

            this.debugService.logSQL(bsql, rawSQL, timeCost);
        }

        //do ORM      
        if (rs.next()) {
            if (loader == null) {
                return m.rs2Object(rs, bsql.getResultClass());
            } else {
                return loader.rs2Object(m, rs);
            }
        } else {
            if (bsql.isExceptionOnNoRecordFound()) {
                throw new DaoException("record not found for the query:[" + rawSQL + "], params:["
                        + bsql.getBindedParams() + "].");
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new JDBCException("Error Code:" + e.getErrorCode() + ", sql:" + rawSQL, e, e.getSQLState());
    } finally {
        CloseUtil.close(rs);
        CloseUtil.close(pstm);
    }
}

From source file:org.guzz.transaction.AbstractTranSessionImpl.java

/**
 * @param bsql/*from w w w . ja  va  2s  .c  o m*/
 * @param startPos 1
 * @param maxSize
 **/
public List list(BindedCompiledSQL bsql, int startPos, int maxSize) {
    ObjectMapping m = bsql.getCompiledSQLToRun().getMapping();
    String rawSQL = bsql.getSQLToRun();
    if (m == null) {
        throw new ORMException("ObjectMapping is null. sql is:" + rawSQL);
    }

    RowDataLoader loader = bsql.getRowDataLoader();

    DBGroup db = m.getDbGroup();
    Dialect dialect = m.getDbGroup().getDialect();

    //?
    LockMode lock = bsql.getLockMode();

    if (lock == LockMode.UPGRADE) {
        rawSQL = dialect.getForUpdateString(rawSQL);
    } else if (lock == LockMode.UPGRADE_NOWAIT) {
        rawSQL = dialect.getForUpdateNoWaitString(rawSQL);
    }

    //TODO: check if the defaultDialect supports prepared bind in limit clause, and put the limit to compiledSQL

    //add limit clause.   
    if (!(startPos == 1 && maxSize == Integer.MAX_VALUE)) {
        rawSQL = db.getDialect().getLimitedString(rawSQL, startPos - 1, maxSize);
    }

    boolean measureTime = this.debugService.isMeasureTime();
    long startTime = 0L;
    if (measureTime) {
        startTime = System.nanoTime();
    }

    PreparedStatement pstm = null;
    ResultSet rs = null;

    try {
        Connection conn = getConnection(db, bsql.getTableCondition());
        pstm = conn.prepareStatement(rawSQL);
        this.applyQueryTimeout(pstm);

        bsql.prepareNamedParams(db.getDialect(), pstm);

        rs = pstm.executeQuery();

        if (this.debugService.isLogSQL()) {
            long timeCost = 0;
            if (measureTime) {
                timeCost = System.nanoTime() - startTime;
            }

            this.debugService.logSQL(bsql, rawSQL, timeCost);
        }

        //do ORM
        LinkedList results = new LinkedList();

        while (rs.next()) {
            if (loader == null) {
                results.addLast(m.rs2Object(rs, bsql.getResultClass()));
            } else {
                results.addLast(loader.rs2Object(m, rs));
            }
        }

        return results;
    } catch (SQLException e) {
        throw new JDBCException("Error Code:" + e.getErrorCode() + ", sql:" + rawSQL, e, e.getSQLState());
    } finally {
        CloseUtil.close(rs);
        CloseUtil.close(pstm);
    }
}

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

public boolean isDataError(String connectionString, SQLException ex) {
    String sqlState = Strings.nullToEmpty(ex.getSQLState());
    String errorCode = String.valueOf(ex.getErrorCode());
    if (sqlState.equals(MYSQL_GENERAL_ERROR) && connectionString.contains(":mysql")) {
        return MYSQL_DATA_ERROR_ERROR_CODES.containsKey(errorCode);
    } else if (sqlState.length() >= 2 && STANDARD_DATA_ERROR_SQLSTATES.containsKey(sqlState.substring(0, 2))) {
        return true;
    }//from   w  w w  .  ja  v  a 2s .  c o m
    return false;
}