Java Code Examples for com.mysql.cj.jdbc.exceptions.SQLError#createSQLException()

The following examples show how to use com.mysql.cj.jdbc.exceptions.SQLError#createSQLException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionImpl.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean isReadOnly(boolean useSessionStatus) throws SQLException {
    if (useSessionStatus && !this.session.isClosed() && versionMeetsMinimum(5, 6, 5) && !this.useLocalSessionState.getValue()
            && this.readOnlyPropagatesToServer.getValue()) {
        try {
            String s = this.session.queryServerVariable(versionMeetsMinimum(8, 0, 3) || (versionMeetsMinimum(5, 7, 20) && !versionMeetsMinimum(8, 0, 0))
                    ? "@@session.transaction_read_only" : "@@session.tx_read_only");
            if (s != null) {
                return Integer.parseInt(s) != 0; // mysql has a habit of tri+ state booleans
            }
        } catch (PasswordExpiredException ex) {
            if (this.disconnectOnExpiredPasswords.getValue()) {
                throw SQLError.createSQLException(Messages.getString("Connection.16"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex,
                        getExceptionInterceptor());
            }
        }
    }

    return this.readOnly;
}
 
Example 2
Source File: ConnectionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests to see if the connection is in Read Only Mode.
 * 
 * @param useSessionStatus
 *            in some cases, for example when restoring connection with autoReconnect=true,
 *            we can rely only on saved readOnly state, so use useSessionStatus=false in that case
 * 
 * @return true if the connection is read only
 * @exception SQLException
 *                if a database access error occurs
 */
public boolean isReadOnly(boolean useSessionStatus) throws SQLException {
    if (useSessionStatus && !this.session.isClosed() && versionMeetsMinimum(5, 6, 5) && !this.useLocalSessionState.getValue()
            && this.readOnlyPropagatesToServer.getValue()) {
        try {
            String s = this.session.queryServerVariable(versionMeetsMinimum(8, 0, 3) || (versionMeetsMinimum(5, 7, 20) && !versionMeetsMinimum(8, 0, 0))
                    ? "@@session.transaction_read_only" : "@@session.tx_read_only");
            if (s != null) {
                return Integer.parseInt(s) != 0; // mysql has a habit of tri+ state booleans
            }
        } catch (PasswordExpiredException ex) {
            if (this.disconnectOnExpiredPasswords.getValue()) {
                throw SQLError.createSQLException(Messages.getString("Connection.16"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex,
                        getExceptionInterceptor());
            }
        }
    }

    return this.readOnly;
}
 
Example 3
Source File: Blob.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Truncates the <code>BLOB</code> value that this <code>Blob</code> object represents to be <code>len</code> bytes in length.
 * <p>
 * <b>Note:</b> If the value specified for <code>len</code> is greater then the length+1 of the <code>BLOB</code> value then the behavior is undefined. Some
 * JDBC drivers may throw a <code>SQLException</code> while other drivers may support this operation.
 * 
 * @param len
 *            the length, in bytes, to which the <code>BLOB</code> value
 *            that this <code>Blob</code> object represents should be truncated
 * @exception SQLException
 *                if there is an error accessing the <code>BLOB</code> value or if len is less than 0
 * @exception SQLFeatureNotSupportedException
 *                if the JDBC driver does not support
 *                this method
 * @since 1.4
 */
public synchronized void truncate(long len) throws SQLException {
    checkClosed();

    if (len < 0) {
        throw SQLError.createSQLException(Messages.getString("Blob.5"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
    }

    if (len > this.binaryData.length) {
        throw SQLError.createSQLException(Messages.getString("Blob.6"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
    }

    // TODO: Do this without copying byte[]s by maintaining some end pointer on the original data

    byte[] newData = new byte[(int) len];
    System.arraycopy(getBinaryData(), 0, newData, 0, (int) len);
    this.binaryData = newData;
}
 
Example 4
Source File: CallableStatementWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setBytes(String parameterName, byte[] x) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            ((CallableStatement) this.wrappedStmt).setBytes(parameterName, x);
        } else {
            throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                    this.exceptionInterceptor);
        }
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
}
 
Example 5
Source File: PreparedStatementWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            ((PreparedStatement) this.wrappedStmt).setTime(parameterIndex, x, cal);
        } else {
            throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                    this.exceptionInterceptor);
        }
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
}
 
Example 6
Source File: UpdatableResultSet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void insertRow() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        if (!this.onInsertRow) {
            throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.7"), getExceptionInterceptor());
        }

        this.inserter.executeUpdate();

        long autoIncrementId = this.inserter.getLastInsertID();
        Field[] fields = this.getMetadata().getFields();
        int numFields = fields.length;
        byte[][] newRow = new byte[numFields][];

        for (int i = 0; i < numFields; i++) {
            if (this.inserter.isNull(i)) {
                newRow[i] = null;
            } else {
                newRow[i] = this.inserter.getBytesRepresentation(i);
            }

            // WARN: This non-variant only holds if MySQL never allows more than one auto-increment key (which is the way it is _today_)
            if (fields[i].isAutoIncrement() && autoIncrementId > 0) {
                newRow[i] = StringUtils.getBytes(String.valueOf(autoIncrementId));
                this.inserter.setBytesNoEscapeNoQuotes(i + 1, newRow[i]);
            }
        }

        Row resultSetRow = new ByteArrayRow(newRow, getExceptionInterceptor());

        // inserter is always a client-side prepared statement, so it's safe to use it
        // with ByteArrayRow for server-side prepared statement too
        refreshRow(this.inserter, resultSetRow);

        this.rowData.addRow(resultSetRow);
        resetInserter();
    }
}
 
Example 7
Source File: CallableStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Reader getCharacterStream(int parameterIndex) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return ((CallableStatement) this.wrappedStmt).getCharacterStream(parameterIndex);
        }
        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                this.exceptionInterceptor);
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }

    return null;
}
 
Example 8
Source File: StatementWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getQueryTimeout() throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return this.wrappedStmt.getQueryTimeout();
        }

        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT,
                this.exceptionInterceptor);
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }

    return 0;
}
 
Example 9
Source File: CallableStatementWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setNull(String parameterName, int sqlType, String typeName) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            ((CallableStatement) this.wrappedStmt).setNull(parameterName, sqlType, typeName);
        } else {
            throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                    this.exceptionInterceptor);
        }
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
}
 
Example 10
Source File: CallableStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Array getArray(String parameterName) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return ((CallableStatement) this.wrappedStmt).getArray(parameterName);
        }
        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                this.exceptionInterceptor);

    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
    return null;
}
 
Example 11
Source File: PreparedStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * JDBC 4.2
 * Same as PreparedStatement.executeUpdate() but returns long instead of int.
 */
public long executeLargeUpdate() throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return ((ClientPreparedStatement) this.wrappedStmt).executeLargeUpdate();
        }

        throw SQLError.createSQLException("No operations allowed after statement closed", MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                this.exceptionInterceptor);
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }

    return -1; // we actually never get here, but the compiler can't figure that out
}
 
Example 12
Source File: StatementWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getResultSetConcurrency() throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return this.wrappedStmt.getResultSetConcurrency();
        }

        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT,
                this.exceptionInterceptor);
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }

    return 0;
}
 
Example 13
Source File: CallableStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public boolean getBoolean(String parameterName) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return ((CallableStatement) this.wrappedStmt).getBoolean(parameterName);
        }
        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                this.exceptionInterceptor);

    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }

    return false;
}
 
Example 14
Source File: CallableStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void setClob(String parameterName, Clob x) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            ((CallableStatement) this.wrappedStmt).setClob(parameterName, x);
        } else {
            throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                    this.exceptionInterceptor);
        }
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
}
 
Example 15
Source File: CallableStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Object getObject(String parameterName, Map<String, Class<?>> typeMap) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return ((CallableStatement) this.wrappedStmt).getObject(parameterName, typeMap);
        }
        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                this.exceptionInterceptor);

    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
    return null;
}
 
Example 16
Source File: StatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public int getResultSetType() throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            return this.wrappedStmt.getResultSetType();
        }

        throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT,
                this.exceptionInterceptor);
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }

    return ResultSet.TYPE_FORWARD_ONLY;
}
 
Example 17
Source File: PreparedStatementWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void setByte(int parameterIndex, byte x) throws SQLException {
    try {
        if (this.wrappedStmt != null) {
            ((PreparedStatement) this.wrappedStmt).setByte(parameterIndex, x);
        } else {
            throw SQLError.createSQLException(Messages.getString("Statement.AlreadyClosed"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                    this.exceptionInterceptor);
        }
    } catch (SQLException sqlEx) {
        checkAndFireConnectionError(sqlEx);
    }
}
 
Example 18
Source File: ConnectionWrapper.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public java.sql.Savepoint setSavepoint() throws SQLException {
    if (isInGlobalTx()) {
        throw SQLError.createSQLException(Messages.getString("ConnectionWrapper.0"), MysqlErrorNumbers.SQL_STATE_INVALID_TRANSACTION_TERMINATION,
                MysqlErrorNumbers.ER_XA_RMERR, this.exceptionInterceptor);
    }

    try {
        return this.mc.setSavepoint();
    } catch (SQLException sqlException) {
        checkAndFireConnectionError(sqlException);
    }

    return null; // we don't reach this code, compiler can't tell
}
 
Example 19
Source File: CallableStatement.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private CallableStatementParam checkIsOutputParam(int paramIndex) throws SQLException {

        synchronized (checkClosed().getConnectionMutex()) {
            if (this.callingStoredFunction) {
                if (paramIndex == 1) {

                    if (this.returnValueParam == null) {
                        this.returnValueParam = new CallableStatementParam("", 0, false, true, MysqlType.VARCHAR.getJdbcType(), "VARCHAR", 0, 0,
                                java.sql.DatabaseMetaData.attributeNullableUnknown, java.sql.DatabaseMetaData.procedureColumnReturn);
                    }

                    return this.returnValueParam;
                }

                // Move to position in output result set
                paramIndex--;
            }

            checkParameterIndexBounds(paramIndex);

            int localParamIndex = paramIndex - 1;

            if (this.placeholderToParameterIndexMap != null) {
                localParamIndex = this.placeholderToParameterIndexMap[localParamIndex];
            }

            CallableStatementParam paramDescriptor = this.paramInfo.getParameter(localParamIndex);

            // We don't have reliable metadata in this case, trust the caller

            if (this.noAccessToProcedureBodies) {
                paramDescriptor.isOut = true;
                paramDescriptor.isIn = true;
                paramDescriptor.inOutModifier = java.sql.DatabaseMetaData.procedureColumnInOut;
            } else if (!paramDescriptor.isOut) {
                throw SQLError.createSQLException(Messages.getString("CallableStatement.9", new Object[] { paramIndex }),
                        MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
            }

            this.hasOutputParams = true;

            return paramDescriptor;
        }
    }
 
Example 20
Source File: Blob.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns an <code>InputStream</code> object that contains a partial <code>Blob</code> value,
 * starting with the byte specified by pos, which is length bytes in length.
 * 
 * @param pos
 *            the offset to the first byte of the partial value to be retrieved.
 *            The first byte in the <code>Blob</code> is at position 1
 * @param length
 *            the length in bytes of the partial value to be retrieved
 * @return <code>InputStream</code> through which the partial <code>Blob</code> value can be read.
 * @throws SQLException
 *             if pos is less than 1 or if pos is greater than the number of bytes
 *             in the <code>Blob</code> or if pos + length is greater than the number of bytes
 *             in the <code>Blob</code>
 * 
 * @exception SQLFeatureNotSupportedException
 *                if the JDBC driver does not support
 *                this method
 * @since 1.6
 */
public synchronized InputStream getBinaryStream(long pos, long length) throws SQLException {
    checkClosed();

    if (pos < 1) {
        throw SQLError.createSQLException(Messages.getString("Blob.2"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
    }

    pos--;

    if (pos > this.binaryData.length) {
        throw SQLError.createSQLException(Messages.getString("Blob.6"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
    }

    if (pos + length > this.binaryData.length) {
        throw SQLError.createSQLException(Messages.getString("Blob.4"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
    }

    return new ByteArrayInputStream(getBinaryData(), (int) pos, (int) length);
}