⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statement.java

📁 在资料浩瀚的互联网中
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            //            if (this.connection.getCacheResultSetMetadata()) {                cachedMetaData = getCachedMetaData(sql);            }            //            // Only apply max_rows to selects            //            if (this.connection.useMaxRows()) {                int rowLimit = -1;                if (isSelect) {                    if (StringUtils.indexOfIgnoreCase(sql, "LIMIT") != -1) { //$NON-NLS-1$                        rowLimit = this.maxRows;                    } else {                        if (this.maxRows <= 0) {                            this.connection.execSQL(this,                                "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, //$NON-NLS-1$                                null, java.sql.ResultSet.TYPE_FORWARD_ONLY,                                java.sql.ResultSet.CONCUR_READ_ONLY, false,                                false, this.currentCatalog, true); //$NON-NLS-1$                        } else {                            this.connection.execSQL(this,                                "SET OPTION SQL_SELECT_LIMIT=" + this.maxRows, //$NON-NLS-1$                                -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY,                                java.sql.ResultSet.CONCUR_READ_ONLY, false,                                false, this.currentCatalog, true); //$NON-NLS-1$                        }                    }                } else {                    this.connection.execSQL(this,                        "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, null, //$NON-NLS-1$                        java.sql.ResultSet.TYPE_FORWARD_ONLY,                        java.sql.ResultSet.CONCUR_READ_ONLY, false, false,                        this.currentCatalog, true); //$NON-NLS-1$                }                // Finally, execute the query                rs = this.connection.execSQL(this, sql, rowLimit, null,                        this.resultSetType, this.resultSetConcurrency,                        createStreamingResultSet(), isSelect,                        this.currentCatalog, (cachedMetaData == null));            } else {                rs = this.connection.execSQL(this, sql, -1, null,                        this.resultSetType, this.resultSetConcurrency,                        createStreamingResultSet(), isSelect,                        this.currentCatalog, (cachedMetaData == null));            }            if (oldCatalog != null) {                this.connection.setCatalog(oldCatalog);            }        }        this.lastInsertId = rs.getUpdateID();        if (rs != null) {            this.results = rs;            rs.setFirstCharOfQuery(firstNonWsChar);            if (rs.reallyResult()) {                if (cachedMetaData != null) {                    initializeResultsMetadataFromCache(sql, cachedMetaData,                        this.results);                } else {                    if (this.connection.getCacheResultSetMetadata()) {                        initializeResultsMetadataFromCache(sql,                            null /* will be created */, this.results);                    }                }            }        }        return ((rs != null) && rs.reallyResult());    }    /**     * @see Statement#execute(String, int)     */    public boolean execute(String sql, int returnGeneratedKeys)        throws SQLException {        if (returnGeneratedKeys == java.sql.Statement.RETURN_GENERATED_KEYS) {            checkClosed();            synchronized (this.connection.getMutex()) {                // If this is a 'REPLACE' query, we need to be able to parse                // the 'info' message returned from the server to determine                // the actual number of keys generated.                boolean readInfoMsgState = this.connection.isReadInfoMsgEnabled();                this.connection.setReadInfoMsgEnabled(true);                try {                    return execute(sql);                } finally {                    this.connection.setReadInfoMsgEnabled(readInfoMsgState);                }            }        }        return execute(sql);    }    /**     * @see Statement#execute(String, int[])     */    public boolean execute(String sql, int[] generatedKeyIndices)        throws SQLException {        if ((generatedKeyIndices != null) && (generatedKeyIndices.length > 0)) {            checkClosed();            synchronized (this.connection.getMutex()) {                // If this is a 'REPLACE' query, we need to be able to parse                // the 'info' message returned from the server to determine                // the actual number of keys generated.                boolean readInfoMsgState = this.connection.isReadInfoMsgEnabled();                this.connection.setReadInfoMsgEnabled(true);                try {                    return execute(sql);                } finally {                    this.connection.setReadInfoMsgEnabled(readInfoMsgState);                }            }        }        return execute(sql);    }    /**     * @see Statement#execute(String, String[])     */    public boolean execute(String sql, String[] generatedKeyNames)        throws SQLException {        if ((generatedKeyNames != null) && (generatedKeyNames.length > 0)) {            checkClosed();            synchronized (this.connection.getMutex()) {                // If this is a 'REPLACE' query, we need to be able to parse                // the 'info' message returned from the server to determine                // the actual number of keys generated.                boolean readInfoMsgState = this.connection.isReadInfoMsgEnabled();                this.connection.setReadInfoMsgEnabled(true);                try {                    return execute(sql);                } finally {                    this.connection.setReadInfoMsgEnabled(readInfoMsgState);                }            }        }        return execute(sql);    }    /**     * JDBC 2.0 Submit a batch of commands to the database for execution. This     * method is optional.     *     * @return an array of update counts containing one element for each     *         command in the batch.  The array is ordered according to the     *         order in which commands were inserted into the batch     *     * @exception SQLException if a database-access error occurs, or the driver     *            does not support batch statements     * @throws java.sql.BatchUpdateException DOCUMENT ME!     */    public synchronized int[] executeBatch() throws SQLException {        if (this.connection.isReadOnly()) {            throw new SQLException(Messages.getString("Statement.34") //$NON-NLS-1$                 +Messages.getString("Statement.35"), //$NON-NLS-1$                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$        }        if (this.results != null) {        	if (!this.connection.getHoldResultsOpenOverStatementClose()) {        		this.results.realClose(false);        	}        }        try {            int[] updateCounts = null;            if (this.batchedArgs != null) {                int nbrCommands = this.batchedArgs.size();                updateCounts = new int[nbrCommands];                for (int i = 0; i < nbrCommands; i++) {                    updateCounts[i] = -3;                }                SQLException sqlEx = null;                int commandIndex = 0;                for (commandIndex = 0; commandIndex < nbrCommands;                        commandIndex++) {                    try {                        updateCounts[commandIndex] = executeUpdate((String) this.batchedArgs.get(                                    commandIndex));                    } catch (SQLException ex) {                        updateCounts[commandIndex] = EXECUTE_FAILED;                        if (this.connection.getContinueBatchOnError()) {                            sqlEx = ex;                        } else {                            int[] newUpdateCounts = new int[commandIndex];                            System.arraycopy(updateCounts, 0, newUpdateCounts,                                0, commandIndex);                            throw new java.sql.BatchUpdateException(ex.getMessage(),                                ex.getSQLState(), ex.getErrorCode(),                                newUpdateCounts);                        }                    }                }                if (sqlEx != null) {                    throw new java.sql.BatchUpdateException(sqlEx.getMessage(),                        sqlEx.getSQLState(), sqlEx.getErrorCode(), updateCounts);                }            }            return (updateCounts != null) ? updateCounts : new int[0];        } finally {            clearBatch();        }    }    /**     * Execute a SQL statement that retruns a single ResultSet     *     * @param sql typically a static SQL SELECT statement     *     * @return a ResulSet that contains the data produced by the query     *     * @exception SQLException if a database access error occurs     */    public synchronized java.sql.ResultSet executeQuery(String sql)        throws SQLException {        checkNullOrEmptyQuery(sql);        checkClosed();        if (this.doEscapeProcessing) {        	Object escapedSqlResult = EscapeProcessor.escapeSQL(sql,        			this.connection.serverSupportsConvertFn());        	        	if (escapedSqlResult instanceof String) {        		sql = (String)escapedSqlResult;        	} else {        		sql = ((EscapeProcessorResult)escapedSqlResult).escapedSql;        	}        }        char firstStatementChar = StringUtils.firstNonWsCharUc(sql);        checkForDml(sql, firstStatementChar);        if (this.results != null) {        	if (!this.connection.getHoldResultsOpenOverStatementClose()) {        		this.results.realClose(false);        	}        }        CachedResultSetMetaData cachedMetaData = null;        // If there isn't a limit clause in the SQL        // then limit the number of rows to return in        // an efficient manner. Only do this if        // setMaxRows() hasn't been used on any Statements        // generated from the current Connection (saves        // a query, and network traffic).        synchronized (this.connection.getMutex()) {            String oldCatalog = null;            if (!this.connection.getCatalog().equals(this.currentCatalog)) {                oldCatalog = this.connection.getCatalog();                this.connection.setCatalog(this.currentCatalog);            }            //            // Check if we have cached metadata for this query...            //            if (this.connection.getCacheResultSetMetadata()) {                cachedMetaData = getCachedMetaData(sql);            }            if (this.connection.useMaxRows()) {                // We need to execute this all together                // So synchronize on the Connection's mutex (because                // even queries going through there synchronize                // on the connection                if (StringUtils.indexOfIgnoreCase(sql, "LIMIT") != -1) { //$NON-NLS-1$                    this.results = this.connection.execSQL(this, sql,                            this.maxRows, null, this.resultSetType,                            this.resultSetConcurrency,                            createStreamingResultSet(), true,                            this.currentCatalog, (cachedMetaData == null));                } else {                    if (this.maxRows <= 0) {                        this.connection.execSQL(this,                            "SET OPTION SQL_SELECT_LIMIT=DEFAULT", -1, null, //$NON-NLS-1$                            java.sql.ResultSet.TYPE_FORWARD_ONLY,                            java.sql.ResultSet.CONCUR_READ_ONLY, false, false,                            this.currentCatalog, true); //$NON-NLS-1$                    } else {                        this.connection.execSQL(this,                            "SET OPTION SQL_SELECT_LIMIT=" + this.maxRows, -1, //$NON-NLS-1$                            null, java.sql.ResultSet.TYPE_FORWARD_ONLY,                            java.sql.ResultSet.CONCUR_READ_ONLY, false, false,                            this.currentCatalog, true); //$NON-NLS-1$                    }                    this.results = this.connection.execSQL(this, sql, -1, null,                            this.resultSetType, this.resultSetConcurrency,                            createStreamingResultSet(), true,                            this.currentCatalog, (cachedMetaData == null));                    if (oldCatalog != null) {                        this.connection.setCatalog(oldCatalog);                    }                }            } else {                this.results = this.connection.execSQL(this, sql, -1, null,                        this.resultSetType, this.resultSetConcurrency,                        createStreamingResultSet(), true, this.currentCatalog,                        (cachedMetaData == null));            }            if (oldCatalog != null) {                this.connection.setCatalog(oldCatalog);            }        }        this.lastInsertId = this.results.getUpdateID();        /*        if (!this.results.reallyResult()) {            if (!this.connection.getAutoCommit()) {                this.connection.rollback();            }            throw new SQLException(Messages.getString("Statement.40"), //$NON-NLS-1$                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$        }*/        if (cachedMetaData != null) {            initializeResultsMetadataFromCache(sql, cachedMetaData, this.results);        } else {            if (this.connection.getCacheResultSetMetadata()) {                initializeResultsMetadataFromCache(sql,                    null /* will be created */, this.results);            }        }        return this.results;    }    /**     * Execute a SQL INSERT, UPDATE or DELETE statement.  In addition SQL     * statements that return nothing such as SQL DDL statements can be     * executed Any IDs generated for AUTO_INCREMENT fields can be retrieved     * by casting this Statement to org.gjt.mm.mysql.Statement and calling the     * getLastInsertID() method.     *     * @param sql a SQL statement     *     * @return either a row count, or 0 for SQL commands     *     * @exception SQLException if a database access error occurs     */    public synchronized int executeUpdate(String sql) throws SQLException {        checkNullOrEmptyQuery(sql);        checkClosed();        if (this.connection.isReadOnly()) {            throw new SQLException(Messages.getString("Statement.42") //$NON-NLS-1$                 +Messages.getString("Statement.43"), //$NON-NLS-1$                SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$        }        if (StringUtils.startsWithIgnoreCaseAndWs(sql, "select")) { //$NON-NLS-1$            throw new SQLException(Messages.getString("Statement.46"), //$NON-NLS-1$                "01S03"); //$NON-NLS-1$        }        char firstStatementChar = StringUtils.firstNonWsCharUc(sql);        if (this.doEscapeProcessing) {        	Object escapedSqlResult = EscapeProcessor.escapeSQL(sql,        			this.connection.serverSupportsConvertFn());        	        	if (escapedSqlResult instanceof String) {        		sql = (String)escapedSqlResult;        	} else {        		sql = ((EscapeProcessorResult)escapedSqlResult).escapedSql;        	}        }        if (this.results != null) {        	if (!this.connection.getHoldResultsOpenOverStatementClose()) {        		this.results.realClose(false);        	}        }        // The checking and changing of catalogs        // must happen in sequence, so synchronize        // on the same mutex that _conn is using        ResultSet rs = null;        synchronized (this.connection.getMutex()) {            String oldCatalog = null;            if (!this.connection.getCatalog().equals(this.currentCatalog)) {                oldCatalog = this.connection.getCatalog();                this.connection.setCatalog(this.currentCatalog);            }            //            // Only apply max_rows to selects            //            if (this.connection.useMaxRows()) {                this.connection.execSQL(this,                    "SET OPTION SQL_SELECT_LIMIT=DEFAULT", //$NON-NLS-1$                    -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY,                    java.sql.ResultSet.CONCUR_READ_ONLY, false, false,                    this.currentCatalog, true);            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -