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

📄 preparedstatement.java

📁 基于b/s的网上书店
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     *
     * @return a ResultSet that contains the data produced by the query - never
     *         null
     *
     * @throws SQLException if a database error occurs.
     */
    public synchronized java.sql.ResultSet executeQuery()
        throws SQLException {
        checkClosed();

        if ((this.firstCharOfStmt == 'I') || (this.firstCharOfStmt == 'U')
                || (this.firstCharOfStmt == 'D')
                || (this.firstCharOfStmt == 'A')
                || (this.firstCharOfStmt == 'C')) {
            if (StringUtils.startsWithIgnoreCaseAndWs(this.originalSql, "INSERT")
                    || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql,
                        "UPDATE")
                    || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql,
                        "DELETE")
                    || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql,
                        "DROP")
                    || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql,
                        "CREATE")
                    || StringUtils.startsWithIgnoreCaseAndWs(this.originalSql,
                        "ALTER")) {
                throw new SQLException("Can not issue data manipulation statements with executeQuery()",
                    SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
            }
        }

        // We need to execute this all together
        // So synchronize on the Connection's mutex (because
        // even queries going through there synchronize
        // on the same mutex.
        synchronized (connection.getMutex()) {
            Buffer sendPacket = fillSendPacket();

            if (this.results != null) {
                this.results.close();
            }

            String oldCatalog = null;

            if (!this.connection.getCatalog().equals(currentCatalog)) {
                oldCatalog = this.connection.getCatalog();
                this.connection.setCatalog(currentCatalog);
            }

            if (this.connection.useMaxRows()) {
                // 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).
                if (hasLimitClause) {
                    results = this.connection.execSQL((String) null, maxRows,
                            sendPacket, resultSetConcurrency,
                            createStreamingResultSet(), true,
                            this.currentCatalog);
                } else {
                    if (maxRows <= 0) {
                        this.connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT",
                            -1, this.currentCatalog);
                    } else {
                        this.connection.execSQL("SET OPTION SQL_SELECT_LIMIT="
                            + maxRows, -1, this.currentCatalog);
                    }

                    this.results = this.connection.execSQL(null, -1,
                            sendPacket, resultSetConcurrency,
                            createStreamingResultSet(), true,
                            this.currentCatalog);

                    if (oldCatalog != null) {
                        this.connection.setCatalog(oldCatalog);
                    }
                }
            } else {
                this.results = this.connection.execSQL(null, -1, sendPacket,
                        resultSetConcurrency, createStreamingResultSet(), true,
                        this.currentCatalog);
            }

            if (oldCatalog != null) {
                this.connection.setCatalog(oldCatalog);
            }
        }

        lastInsertId = this.results.getUpdateID();
        nextResults = this.results;
        this.results.setConnection(connection);
        this.results.setResultSetType(resultSetType);
        this.results.setResultSetConcurrency(resultSetConcurrency);
        this.results.setStatement(this);

        return (java.sql.ResultSet) 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.
     *
     * @return either the row count for INSERT, UPDATE or DELETE; or 0 for SQL
     *         statements that return nothing.
     *
     * @throws SQLException if a database access error occurs
     */
    public synchronized int executeUpdate() throws SQLException {
        return executeUpdate(parameterValues, parameterStreams, isStream,
            streamLengths, isNull);
    }

    /**
     * Returns this PreparedStatement represented as a string.
     *
     * @return this PreparedStatement represented as a string.
     *
     * @throws RuntimeException if an error occurs
     */
    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append(super.toString());
        buf.append(": ");

        try {
            for (int i = 0; i < parameterValues.length; ++i) {
                if (this.charEncoding != null) {
                    buf.append(new String(staticSqlStrings[i], this.charEncoding));
                } else {
                    buf.append(new String(staticSqlStrings[i]));
                }

                if ((parameterValues[i] == null) && !isStream[i]) {
                    buf.append("** NOT SPECIFIED **");
                } else if (isStream[i]) {
                    buf.append("** STREAM DATA **");
                } else {
                    if (this.charConverter != null) {
                        buf.append(this.charConverter.toString(
                                parameterValues[i]));
                    } else {
                        if (this.charEncoding != null) {
                            buf.append(new String(parameterValues[i],
                                    this.charEncoding));
                        } else {
                            buf.append(StringUtils.toAsciiString(
                                    parameterValues[i]));
                        }
                    }
                }
            }

            if (this.charEncoding != null) {
                buf.append(new String(
                        staticSqlStrings[parameterValues.length],
                        this.charEncoding));
            } else {
                buf.append(staticSqlStrings[parameterValues.length]);
            }
        } catch (UnsupportedEncodingException uue) {
            throw new RuntimeException("Unsupported character encoding '"
                + this.charEncoding + "'");
        }

        return buf.toString();
    }

    /**
     * Used by updatable result sets for refreshRow() because the parameter has
     * already been escaped for updater or inserter prepared statements.
     *
     * @param parameterIndex the parameter to set.
     * @param parameterAsBytes the parameter as a string.
     *
     * @throws SQLException if an error occurs
     */
    protected void setBytesNoEscape(int parameterIndex, byte[] parameterAsBytes)
        throws SQLException {
        byte[] parameterWithQuotes = new byte[parameterAsBytes.length + 2];
        parameterWithQuotes[0] = '\'';
        System.arraycopy(parameterAsBytes, 0, parameterWithQuotes, 1,
            parameterAsBytes.length);
        parameterWithQuotes[parameterAsBytes.length + 1] = '\'';

        setInternal(parameterIndex, parameterWithQuotes);
    }

    /**
     * Sets wheather or not this statement should retreive generated keys.
     *
     * @param retrieveGeneratedKeys
     */
    protected void setRetrieveGeneratedKeys(boolean retrieveGeneratedKeys) {
        this.retrieveGeneratedKeys = retrieveGeneratedKeys;
    }

    /**
     * Added to allow batch-updates
     *
     * @param batchedParameterStrings string values used in single statement
     * @param batchedParameterStreams stream values used in single statement
     * @param batchedIsStream flags for streams used in single statement
     * @param batchedStreamLengths lengths of streams to be read.
     * @param batchedIsNull flags for parameters that are null
     *
     * @return the update count
     *
     * @throws SQLException if a database error occurs
     * @throws java.sql.SQLException DOCUMENT ME!
     */
    protected synchronized int executeUpdate(byte[][] batchedParameterStrings,
        InputStream[] batchedParameterStreams, boolean[] batchedIsStream,
        int[] batchedStreamLengths, boolean[] batchedIsNull)
        throws SQLException {
        if (connection.isReadOnly()) {
            throw new SQLException("Connection is read-only. "
                + "Queries leading to data modification are not allowed",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        checkClosed();

        if ((this.firstCharOfStmt == 'S')
                && StringUtils.startsWithIgnoreCaseAndWs(this.originalSql,
                    "SELECT")) {
            throw new java.sql.SQLException("Can not issue executeUpdate() for SELECTs",
                SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
        }

        ResultSet rs = null;

        // The checking and changing of catalogs
        // must happen in sequence, so synchronize
        // on the same mutex that _conn is using
        synchronized (connection.getMutex()) {
            Buffer sendPacket = fillSendPacket(batchedParameterStrings,
                    batchedParameterStreams, batchedIsStream,
                    batchedStreamLengths);

            String oldCatalog = null;

            if (!this.connection.getCatalog().equals(currentCatalog)) {
                oldCatalog = this.connection.getCatalog();
                this.connection.setCatalog(currentCatalog);
            }

            //
            // Only apply max_rows to selects
            //
            if (this.connection.useMaxRows()) {
                this.connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT",
                    -1, this.currentCatalog);
            }

            boolean oldInfoMsgState = false;

            if (this.retrieveGeneratedKeys) {
                oldInfoMsgState = this.connection.isReadInfoMsgEnabled();
                this.connection.setReadInfoMsgEnabled(true);
            }

            rs = this.connection.execSQL(null, -1, sendPacket,
                    resultSetConcurrency, false, false, this.currentCatalog);

            if (this.retrieveGeneratedKeys) {
                this.connection.setReadInfoMsgEnabled(oldInfoMsgState);
            }

            if (oldCatalog != null) {
                this.connection.setCatalog(oldCatalog);
            }
        }

        this.results = rs;

        rs.setFirstCharOfQuery(this.firstCharOfStmt);

        updateCount = rs.getUpdateCount();

        int truncatedUpdateCount = 0;

        if (updateCount > Integer.MAX_VALUE) {
            truncatedUpdateCount = Integer.MAX_VALUE;
        } else {
            truncatedUpdateCount = (int) updateCount;
        }

        lastInsertId = rs.getUpdateID();
        this.results = rs;

        return truncatedUpdateCount;
    }

    byte[] getBytes(int parameterIndex) throws SQLException {
        if (isStream[parameterIndex]) {
            return streamToBytes(parameterStreams[parameterIndex], false,
                streamLengths[parameterIndex],
                this.connection.useStreamLengthsInPrepStmts());
        } else {
            byte[] parameterVal = parameterValues[parameterIndex];

            if (parameterVal == null) {
                return null;
            }

            if ((parameterVal[0] == '\'')
                    && (parameterVal[parameterVal.length - 1] == '\'')) {
                byte[] valNoQuotes = new byte[parameterVal.length - 2];
                System.arraycopy(parameterVal, 1, valNoQuotes, 0,
                    parameterVal.length - 2);

                return valNoQuotes;
            } else {
                return parameterVal;
            }
        }
    }

    boolean isNull(int paramIndex) {
        return isNull[paramIndex];
    }

    ParseInfo getParseInfo() {
        return this.parseInfo;
    }

    /**
     * Sets the concurrency for result sets generated by this statement
     *
     * @param concurrencyFlag the result set concurrency flag from the
     *        ResultSet interface.
     */
    void setResultSetConcurrency(int concurrencyFlag) {
        resultSetConcurrency = concurrencyFlag;
    }

    /**
     * Sets the result set type for result sets generated by this statement
     *
     * @param typeFlag the result set type from the ResultSet interface
     */
    void setResultSetType(int typeFlag) {
        resultSetType = typeFlag;
    }

    private final String getDateTimePattern(String dt, boolean toTime)
        throws Exception {
        //
        

⌨️ 快捷键说明

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