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

📄 updatableresultset.java

📁 基于b/s的网上书店
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        if (connection.useUnicode()) {
            characterEncoding = connection.getEncoding();
        }

        //
        // FIXME: Use internal routines where possible for character
        //        conversion!
        try {
            int numKeys = primaryKeyIndicies.size();

            if (numKeys == 1) {
                int index = ((Integer) primaryKeyIndicies.get(0)).intValue();
                String currentVal = ((characterEncoding == null)
                    ? new String(thisRow[index])
                    : new String(thisRow[index], characterEncoding));
                deleter.setString(1, currentVal);
            } else {
                for (int i = 0; i < numKeys; i++) {
                    int index = ((Integer) primaryKeyIndicies.get(i)).intValue();
                    String currentVal = ((characterEncoding == null)
                        ? new String(thisRow[index])
                        : new String(thisRow[index], characterEncoding));
                    deleter.setString(i + 1, currentVal);
                }
            }

            deleter.executeUpdate();
            rowData.removeRow(rowData.getCurrentRowNumber());
        } catch (java.io.UnsupportedEncodingException encodingEx) {
            throw new SQLException("Unsupported character encoding '"
                + connection.getEncoding() + "'");
        }
    }

    /**
     * JDBC 2.0
     * 
     * <p>
     * Moves to the first row in the result set.
     * </p>
     *
     * @return true if on a valid row, false if no rows in the result set.
     *
     * @exception SQLException if a database-access error occurs, or result set
     *            type is TYPE_FORWARD_ONLY.
     */
    public synchronized boolean first() throws SQLException {
        return super.first();
    }

    /**
     * JDBC 2.0 Insert the contents of the insert row into the result set and
     * the database.  Must be on the insert row when this method is called.
     *
     * @exception SQLException if a database-access error occurs, if called
     *            when not on the insert row, or if all non-nullable columns
     *            in the insert row have not been given a value
     */
    public synchronized void insertRow() throws SQLException {
        if (!onInsertRow) {
            throw new SQLException("Not on insert row");
        } else {
            inserter.executeUpdate();

            int numPrimaryKeys = 0;

            if (primaryKeyIndicies != null) {
                numPrimaryKeys = primaryKeyIndicies.size();
            }

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

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

                if ((numPrimaryKeys == 1) && fields[i].isPrimaryKey()
                        && (autoIncrementId > 0)) {
                    newRow[i] = String.valueOf(autoIncrementId).getBytes();
                }
            }

            rowData.addRow(newRow);
            resetInserter();
        }
    }

    /**
     * JDBC 2.0
     * 
     * <p>
     * Moves to the last row in the result set.
     * </p>
     *
     * @return true if on a valid row, false if no rows in the result set.
     *
     * @exception SQLException if a database-access error occurs, or result set
     *            type is TYPE_FORWARD_ONLY.
     */
    public synchronized boolean last() throws SQLException {
        return super.last();
    }

    /**
     * JDBC 2.0 Move the cursor to the remembered cursor position, usually the
     * current row.  Has no effect unless the cursor is on the insert  row.
     *
     * @exception SQLException if a database-access error occurs, or the result
     *            set is not updatable
     * @throws SQLException if the ResultSet is not updatable or some other
     *         error occurs
     */
    public synchronized void moveToCurrentRow() throws SQLException {
        if (!isUpdatable) {
            throw new NotUpdatable();
        }

        if (this.onInsertRow) {
            onInsertRow = false;
            this.thisRow = this.savedCurrentRow;
        }
    }

    /**
     * JDBC 2.0 Move to the insert row.  The current cursor position is
     * remembered while the cursor is positioned on the insert row. The insert
     * row is a special row associated with an updatable result set.  It is
     * essentially a buffer where a new row may be constructed by calling the
     * updateXXX() methods prior to  inserting the row into the result set.
     * Only the updateXXX(), getXXX(), and insertRow() methods may be  called
     * when the cursor is on the insert row.  All of the columns in  a result
     * set must be given a value each time this method is called before
     * calling insertRow().  UpdateXXX()must be called before getXXX() on a
     * column.
     *
     * @exception SQLException if a database-access error occurs, or the result
     *            set is not updatable
     * @throws NotUpdatable DOCUMENT ME!
     */
    public synchronized void moveToInsertRow() throws SQLException {
        if (!this.isUpdatable) {
            throw new NotUpdatable();
        }

        if (this.inserter == null) {
            generateStatements();
            this.inserter = (com.mysql.jdbc.PreparedStatement) connection
                .prepareStatement(this.insertSQL);
            
            if (this.inserter.getMaxRows() != 0) {
            	this.inserter.setMaxRows(0);
            }
            
            extractDefaultValues();
            resetInserter();
        } else {
            resetInserter();
        }

		int numFields = this.fields.length;
		
		this.onInsertRow = true;
		this.doingUpdates = false;
		this.savedCurrentRow = this.thisRow;
		this.thisRow = new byte[numFields][];
			   
        for (int i = 0; i < numFields; i++) {
            if (this.defaultColumnValue[i] != null) {
                this.inserter.setBytes(i + 1, this.defaultColumnValue[i]);
                
                // This value _could_ be changed from a getBytes(), so we
                // need a copy....
                byte[] defaultValueCopy = new byte[this.defaultColumnValue[i].length];
                System.arraycopy(defaultColumnValue[i], 0, defaultValueCopy, 0, defaultValueCopy.length);
                this.thisRow[i] = defaultValueCopy;
            } else {
                this.inserter.setNull(i + 1, java.sql.Types.NULL);
                this.thisRow[i] = null;
            }
        }
    }

    /**
     * A ResultSet is initially positioned before its first row, the first call
     * to next makes the first row the current row; the second call makes the
     * second row the current row, etc.
     * 
     * <p>
     * If an input stream from the previous row is open, it is implicitly
     * closed.  The ResultSet's warning chain is cleared when a new row is
     * read
     * </p>
     *
     * @return true if the new current is valid; false if there are no more
     *         rows
     *
     * @exception java.sql.SQLException if a database access error occurs
     */
    public synchronized boolean next() throws java.sql.SQLException {
        return super.next();
    }

    /**
     * The prev method is not part of JDBC, but because of the architecture of
     * this driver it is possible to move both forward and backward within the
     * result set.
     * 
     * <p>
     * If an input stream from the previous row is open, it is implicitly
     * closed.  The ResultSet's warning chain is cleared when a new row is
     * read
     * </p>
     *
     * @return true if the new current is valid; false if there are no more
     *         rows
     *
     * @exception java.sql.SQLException if a database access error occurs
     */
    public synchronized boolean prev() throws java.sql.SQLException {
        return super.prev();
    }

    /**
     * JDBC 2.0
     * 
     * <p>
     * Moves to the previous row in the result set.
     * </p>
     * 
     * <p>
     * Note: previous() is not the same as relative(-1) since it makes sense to
     * call previous() when there is no current row.
     * </p>
     *
     * @return true if on a valid row, false if off the result set.
     *
     * @exception SQLException if a database-access error occurs, or result set
     *            type is TYPE_FORWAR_DONLY.
     */
    public synchronized boolean previous() throws SQLException {
        return super.previous();
    }

    /**
     * JDBC 2.0 Refresh the value of the current row with its current value in
     * the database.  Cannot be called when on the insert row. The
     * refreshRow() method provides a way for an application to  explicitly
     * tell the JDBC driver to refetch a row(s) from the database.  An
     * application may want to call refreshRow() when  caching or prefetching
     * is being done by the JDBC driver to fetch the latest value of a row
     * from the database.  The JDBC driver  may actually refresh multiple rows
     * at once if the fetch size is  greater than one.  All values are
     * refetched subject to the transaction isolation  level and cursor
     * sensitivity.  If refreshRow() is called after calling updateXXX(), but
     * before calling updateRow() then the updates made to the row are lost.
     * Calling refreshRow() frequently will likely slow performance.
     *
     * @exception SQLException if a database-access error occurs, or if called
     *            when on the insert row.
     * @throws NotUpdatable DOCUMENT ME!
     */
    public synchronized void refreshRow() throws SQLException {
        if (!isUpdatable) {
            throw new NotUpdatable();
        }

        if (onInsertRow) {
            throw new SQLException(
                "Can not call refreshRow() when on insert row");
        } else if (rowData.size() == 0) {
            throw new SQLException("Can't refreshRow() on empty result set");
        } else if (isBeforeFirst()) {
            throw new SQLException(
                "Before start of result set. Can not call refreshRow().");
        } else if (isAfterLast()) {
            throw new SQLException(
                "After end of result set. Can not call refreshRow().");
        }

        if (refresher == null) {
            if (refreshSQL == null) {
                generateStatements();
            }

            refresher = (com.mysql.jdbc.PreparedStatement) connection
                .prepareStatement(refreshSQL);
            
            if (refresher.getMaxRows() != 0) {
            	refresher.setMaxRows(0);
            }
        }

        refresher.clearParameters();

        int numKeys = primaryKeyIndicies.size();

        if (numKeys == 1) {
            byte[] dataFrom = null;
            int index = ((Integer) primaryKeyIndicies.get(0)).intValue();

            if (!doingUpdates) {
                dataFrom = thisRow[index];
            } else {
                dataFrom = updater.getBytes(index);

                // Primary keys not set?
                if (updater.isNull(index) || (dataFrom.length == 0)) {
                    dataFrom = thisRow[index];
                }
            }

            refresher.setBytesNoEscape(1, dataFrom);
        } else {
            for (int i = 0; i < numKeys; i++) {
                byte[] dataFrom = null;
                int index = ((Integer) primaryKeyIndicies.get(i)).intValue();

                if (!doingUpdates) {
                    dataFrom = thisRow[index];
                } else {
                    dataFrom = updater.getBytes(index);

                    // Primary keys not set?
                    if (updater.isNull(index) || (dataFrom.length == 0)) {
                        dataFrom = thisRow[index];
                    }
                }

                refresher.setBytesNoEscape(i + 1, dataFrom);
            }
        }

        java.sql.ResultSet rs = null;

        try {
            rs = refresher.executeQuery();

            int numCols = rs.getMetaData().getColumnCount();

            if (rs.next()) {
                for (int i = 0; i < numCols; i++) {
                    byte[] val = rs.getBytes(i + 1);

                    if ((val == null) || rs.wasNull()) {
                        thisRow[i] = null;
                    } else {
                        thisRow[i] = rs.getBytes(i + 1);
                    }
                }
            } else {
                throw new SQLException("refreshRow() called on row that has been deleted or had primary key changed",
                    SQLError.SQL_STATE_GENERAL_ERROR);
            }
        } finally {
            if (rs != null) {

⌨️ 快捷键说明

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