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

📄 updatableresultset.java

📁 mysql的jdbc驱动
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		if (!this.isUpdatable) {			throw new NotUpdatable();		}		if (this.inserter == null) {			if (this.insertSQL == null) {				generateStatements();			}			this.inserter = this.connection					.clientPrepareStatement(this.insertSQL);			extractDefaultValues();			resetInserter();		} else {			resetInserter();		}		int numFields = this.fields.length;		this.onInsertRow = true;		this.doingUpdates = false;		this.savedCurrentRow = (byte[][]) this.thisRow;		this.thisRow = new byte[numFields][];		for (int i = 0; i < numFields; i++) {			if (this.defaultColumnValue[i] != null) {				Field f = this.fields[i];				switch (f.getMysqlType()) {				case MysqlDefs.FIELD_TYPE_DATE:				case MysqlDefs.FIELD_TYPE_DATETIME:				case MysqlDefs.FIELD_TYPE_NEWDATE:				case MysqlDefs.FIELD_TYPE_TIME:				case MysqlDefs.FIELD_TYPE_TIMESTAMP:					if (this.defaultColumnValue[i].length > 7							&& this.defaultColumnValue[i][0] == (byte) 'C'							&& this.defaultColumnValue[i][1] == (byte) 'U'							&& this.defaultColumnValue[i][2] == (byte) 'R'							&& this.defaultColumnValue[i][3] == (byte) 'R'							&& this.defaultColumnValue[i][4] == (byte) 'E'							&& this.defaultColumnValue[i][5] == (byte) 'N'							&& this.defaultColumnValue[i][6] == (byte) 'T'							&& this.defaultColumnValue[i][7] == (byte) '_') {						this.inserter.setBytesNoEscapeNoQuotes(i + 1,								this.defaultColumnValue[i]);						break;					}				default:					this.inserter.setBytes(i + 1, this.defaultColumnValue[i],							false, false);				}				// 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;			}		}	}	// ---------------------------------------------------------------------	// Updates	// ---------------------------------------------------------------------	/**	 * 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 SQLException	 *                if a database access error occurs	 */	public synchronized boolean next() throws 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 SQLException	 *                if a database access error occurs	 */	public synchronized boolean prev() throws 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();	}	/**	 * Closes this ResultSet, releasing all resources.	 * 	 * @param calledExplicitly	 *            was this called from close()?	 * 	 * @throws SQLException	 *             if an error occurs.	 */	protected void realClose(boolean calledExplicitly) throws SQLException {		SQLException sqlEx = null;		if (this.useUsageAdvisor) {			if ((this.deleter == null) && (this.inserter == null)					&& (this.refresher == null) && (this.updater == null)) {				this.eventSink = ProfileEventSink.getInstance(this.connection);				String message = Messages.getString("UpdatableResultSet.34"); //$NON-NLS-1$				this.eventSink.consumeEvent(new ProfilerEvent(						ProfilerEvent.TYPE_WARN, "", //$NON-NLS-1$						(this.owningStatement == null) ? "N/A" //$NON-NLS-1$								: this.owningStatement.currentCatalog, //$NON-NLS-1$						this.connection.getId(),						(this.owningStatement == null) ? (-1)								: this.owningStatement.getId(), this.resultId,						System.currentTimeMillis(), 0, null,						this.pointOfOrigin, message));			}		}		try {			if (this.deleter != null) {				this.deleter.close();			}		} catch (SQLException ex) {			sqlEx = ex;		}		try {			if (this.inserter != null) {				this.inserter.close();			}		} catch (SQLException ex) {			sqlEx = ex;		}		try {			if (this.refresher != null) {				this.refresher.close();			}		} catch (SQLException ex) {			sqlEx = ex;		}		try {			if (this.updater != null) {				this.updater.close();			}		} catch (SQLException ex) {			sqlEx = ex;		}		super.realClose(calledExplicitly);		if (sqlEx != null) {			throw sqlEx;		}	}	/**	 * 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 {		checkClosed();		if (!this.isUpdatable) {			throw new NotUpdatable();		}		if (this.onInsertRow) {			throw new SQLException(Messages.getString("UpdatableResultSet.8")); //$NON-NLS-1$		} else if (this.rowData.size() == 0) {			throw new SQLException(Messages.getString("UpdatableResultSet.9")); //$NON-NLS-1$		} else if (isBeforeFirst()) {			throw new SQLException(Messages.getString("UpdatableResultSet.10")); //$NON-NLS-1$		} else if (isAfterLast()) {			throw new SQLException(Messages.getString("UpdatableResultSet.11")); //$NON-NLS-1$		}		if (this.refresher == null) {			if (this.refreshSQL == null) {				generateStatements();			}			this.refresher = this.connection					.clientPrepareStatement(this.refreshSQL);		}		this.refresher.clearParameters();		int numKeys = this.primaryKeyIndicies.size();		if (numKeys == 1) {			byte[] dataFrom = null;			int index = ((Integer) this.primaryKeyIndicies.get(0)).intValue();			if (!this.doingUpdates) {				dataFrom = (byte[]) this.thisRow[index];			} else {				dataFrom = this.updater.getBytesRepresentation(index);				// Primary keys not set?				if (this.updater.isNull(index) || (dataFrom.length == 0)) {					dataFrom = (byte[]) this.thisRow[index];				} else {					dataFrom = stripBinaryPrefix(dataFrom);				}			}			this.refresher.setBytesNoEscape(1, dataFrom);		} else {			for (int i = 0; i < numKeys; i++) {				byte[] dataFrom = null;				int index = ((Integer) this.primaryKeyIndicies.get(i))						.intValue();				if (!this.doingUpdates) {					dataFrom = (byte[]) this.thisRow[index];				} else {					dataFrom = this.updater.getBytesRepresentation(index);					// Primary keys not set?					if (this.updater.isNull(index) || (dataFrom.length == 0)) {						dataFrom = (byte[]) this.thisRow[index];					} else {						dataFrom = stripBinaryPrefix(dataFrom);					}				}				this.refresher.setBytesNoEscape(i + 1, dataFrom);			}		}		java.sql.ResultSet rs = null;		try {			rs = this.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()) {						this.thisRow[i] = null;					} else {						this.thisRow[i] = rs.getBytes(i + 1);					}				}			} else {				throw new SQLException(Messages						.getString("UpdatableResultSet.12"), //$NON-NLS-1$						SQLError.SQL_STATE_GENERAL_ERROR); //$NON-NLS-1$			}		} finally {			if (rs != null) {				try {					rs.close();				} catch (SQLException ex) {					; // ignore				}			}		}	}	/**	 * JDBC 2.0	 * 	 * <p>	 * Moves a relative number of rows, either positive or negative. Attempting	 * to move beyond the first/last row in the result set positions the cursor	 * before/after the the first/last row. Calling relative(0) is valid, but	 * does not change the cursor position.	 * </p>	 * 	 * <p>	 * Note: Calling relative(1) is different than calling next() since is makes	 * sense to call next() when there is no current row, for example, when the	 * cursor is positioned before the first row or after the last row of the	 * result set.	 * </p>	 * 	 * @param rows	 *            DOCUMENT ME!	 * 	 * @return true if on a row, false otherwise.	 * 	 * @exception SQLException	 *                if a database-access error occurs, or there is no current	 *                row, or result set type is TYPE_FORWARD_ONLY.	 */	public synchronized boolean relative(int rows) throws SQLException {		return super.relative(rows);	}	private void resetInserter() throws SQLException {		this.inserter.clearParameters();		for (int i = 0; i < this.fields.length; i++) {			this.inserter.setNull(i + 1, 0);		}	}	/**	 * JDBC 2.0 Determine if this row has been deleted. A deleted row may leave	 * a visible "hole" in a result set. This method can be used to detect holes	 * in a result set. The value returned depends on whether or not the result	 * set can detect deletions.	 * 	 * @return true if deleted and deletes are detected	 * 	 * @exception SQLException	 *                if a database-access error occurs	 * @throws NotImplemented	 *             DOCUMENT ME!	 * 	 * @see DatabaseMetaData#deletesAreDetected	 */	public synchronized boolean rowDeleted() throws SQLException {		throw new NotImplemented();	}	/**	 * JDBC 2.0 Determine if the current row has been inserted. The value	 * returned depends on whether or not the result set can detect visible	 * inserts.	 * 	 * @return true if inserted and inserts are detected	 * 	 * @exception SQLException	 *                if a database-access error occurs	 * @throws NotImplemented	 *             DOCUMENT ME!	 * 	 * @see DatabaseMetaData#insertsAreDetected	 */	public synchronized boolean rowInserted() throws SQLException {		throw new NotImplemented();	}	/**	 * JDBC 2.0 Determine if the current row has been updated. The value	 * returned depends on whether or not the result set can detect updates.	 * 	 * @return true if the row has been visibly updated by the owner or another,	 *         and updates are detected	 * 	 * @exception SQLException	 *                if a database-access error occurs	 * @throws NotImplemented	 *             DOCUMENT ME!	 * 	 * @see DatabaseMetaData#updatesAreDetected	 */	public synchronized boolean rowUpdated() throws SQLException {		throw new NotImplemented();	}	/**	 * Sets the concurrency type of this result set	 * 	 * @param concurrencyFlag	 *            the type of concurrency that this ResultSet should support.	 */	protected void setResultSetConcurrency(int concurrencyFlag) {		super.setResultSetConcurrency(concurrencyFlag);		//		// FIXME: Issue warning when asked for updateable result set, but result		// set is not		// updatable		//		// if ((concurrencyFlag == CONCUR_UPDATABLE) && !isUpdatable()) {		// java.sql.SQLWarning warning = new java.sql.SQLWarning(		// NotUpdatable.NOT_UPDATEABLE_MESSAGE);		// }	}	private byte[] stripBinaryPrefix(byte[] dataFrom) {		return StringUtils.stripEnclosure(dataFrom, "_binary'", "'");	}	/**	 * Reset UPDATE prepared statement to value in current row. This_Row MUST	 * point to current, valid row.

⌨️ 快捷键说明

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