?? updatableresultset.java
字號:
/*
Copyright (C) 2002 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mysql.jdbc;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A result set that is updatable.
*
* @author Mark Matthews
*/
public class UpdatableResultSet extends ResultSet {
/** Marker for 'stream' data when doing INSERT rows */
private final static byte[] STREAM_DATA_MARKER = "** STREAM DATA **"
.getBytes();
/** List of primary keys */
private List primaryKeyIndicies = null;
/** PreparedStatement used to delete data */
private com.mysql.jdbc.PreparedStatement deleter = null;
/** PreparedStatement used to insert data */
private com.mysql.jdbc.PreparedStatement inserter = null;
/** PreparedStatement used to refresh data */
private com.mysql.jdbc.PreparedStatement refresher;
/** PreparedStatement used to delete data */
private com.mysql.jdbc.PreparedStatement updater = null;
private SingleByteCharsetConverter charConverter;
private String charEncoding;
private String deleteSQL = null;
private String insertSQL = null;
private String quotedIdChar = null;
private String refreshSQL = null;
private String tableName;
/** SQL for in-place modifcation */
private String updateSQL = null;
/** What is the default value for the column? */
private byte[][] defaultColumnValue;
/** The binary data for the 'current' row */
private byte[][] savedCurrentRow;
/** Is this result set updateable? */
private boolean isUpdatable = false;
/**
* Creates a new UpdatableResultSet object.
*
* @param updateCount DOCUMENT ME!
* @param updateID DOCUMENT ME!
*/
public UpdatableResultSet(long updateCount, long updateID) {
super(updateCount, updateID);
}
// ****************************************************************
//
// END OF PUBLIC INTERFACE
//
// ****************************************************************
/**
* Create a new ResultSet - Note that we create ResultSets to represent the
* results of everything.
*
* @param catalog the database in use when this result set was created
* @param fields an array of Field objects (basically, the ResultSet
* MetaData)
* @param rows Vector of the actual data
* @param conn the status string returned from the back end
*
* @throws SQLException DOCUMENT ME!
*/
public UpdatableResultSet(String catalog, Field[] fields, RowData rows,
com.mysql.jdbc.Connection conn) throws SQLException {
super(catalog, fields, rows, conn);
isUpdatable = checkUpdatability();
}
/**
* Creates a new UpdatableResultSet object.
*
* @param fields DOCUMENT ME!
* @param rows DOCUMENT ME!
*
* @throws SQLException DOCUMENT ME!
*/
public UpdatableResultSet(Field[] fields, RowData rows)
throws SQLException {
super(fields, rows);
isUpdatable = checkUpdatability();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is after the last row in the result set.
* </p>
*
* @return true if after the last row, false otherwise. Returns false when
* the result set contains no rows.
*
* @exception SQLException if a database-access error occurs.
*/
public synchronized boolean isAfterLast() throws SQLException {
return super.isAfterLast();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is before the first row in the result set.
* </p>
*
* @return true if before the first row, false otherwise. Returns false
* when the result set contains no rows.
*
* @exception SQLException if a database-access error occurs.
*/
public synchronized boolean isBeforeFirst() throws SQLException {
return super.isBeforeFirst();
}
/**
* JDBC 2.0 Return the concurrency of this result set. The concurrency
* used is determined by the statement that created the result set.
*
* @return the concurrency type, CONCUR_READ_ONLY, etc.
*
* @exception SQLException if a database-access error occurs
*/
public int getConcurrency() throws SQLException {
return (isUpdatable ? CONCUR_UPDATABLE : CONCUR_READ_ONLY);
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is on the first row of the result set.
* </p>
*
* @return true if on the first row, false otherwise.
*
* @exception SQLException if a database-access error occurs.
*/
public synchronized boolean isFirst() throws SQLException {
return super.isFirst();
}
/**
* JDBC 2.0
*
* <p>
* Determine if the cursor is on the last row of the result set. Note:
* Calling isLast() may be expensive since the JDBC driver might need to
* fetch ahead one row in order to determine whether the current row is
* the last row in the result set.
* </p>
*
* @return true if on the last row, false otherwise.
*
* @exception SQLException if a database-access error occurs.
*/
public synchronized boolean isLast() throws SQLException {
return super.isLast();
}
/**
* JDBC 2.0
*
* <p>
* Move to an absolute row number in the result set.
* </p>
*
* <p>
* If row is positive, moves to an absolute row with respect to the
* beginning of the result set. The first row is row 1, the second is row
* 2, etc.
* </p>
*
* <p>
* If row is negative, moves to an absolute row position with respect to
* the end of result set. For example, calling absolute(-1) positions the
* cursor on the last row, absolute(-2) indicates the next-to-last row,
* etc.
* </p>
*
* <p>
* An attempt to position the cursor beyond the first/last row in the
* result set, leaves the cursor before/after the first/last row,
* respectively.
* </p>
*
* <p>
* Note: Calling absolute(1) is the same as calling first(). Calling
* absolute(-1) is the same as calling last().
* </p>
*
* @param row DOCUMENT ME!
*
* @return true if on the result set, false if off.
*
* @exception SQLException if a database-access error occurs, or row is 0,
* or result set type is TYPE_FORWARD_ONLY.
*/
public synchronized boolean absolute(int row) throws SQLException {
return super.absolute(row);
}
/**
* JDBC 2.0
*
* <p>
* Moves to the end of the result set, just after the last row. Has no
* effect if the result set contains no rows.
* </p>
*
* @exception SQLException if a database-access error occurs, or result set
* type is TYPE_FORWARD_ONLY.
*/
public synchronized void afterLast() throws SQLException {
super.afterLast();
}
/**
* JDBC 2.0
*
* <p>
* Moves to the front of the result set, just before the first row. Has no
* effect if the result set contains no rows.
* </p>
*
* @exception SQLException if a database-access error occurs, or result set
* type is TYPE_FORWARD_ONLY
*/
public synchronized void beforeFirst() throws SQLException {
super.beforeFirst();
}
/**
* JDBC 2.0 The cancelRowUpdates() method may be called after calling an
* updateXXX() method(s) and before calling updateRow() to rollback the
* updates made to a row. If no updates have been made or updateRow()
* has already been called, then this method has no effect.
*
* @exception SQLException if a database-access error occurs, or if called
* when on the insert row.
*/
public synchronized void cancelRowUpdates() throws SQLException {
if (doingUpdates) {
doingUpdates = false;
updater.clearParameters();
}
}
/**
* After this call, getWarnings returns null until a new warning is
* reported for this ResultSet
*
* @exception java.sql.SQLException if a database access error occurs
*/
public synchronized void clearWarnings() throws java.sql.SQLException {
warningChain = null;
}
/**
* In some cases, it is desirable to immediately release a ResultSet
* database and JDBC resources instead of waiting for this to happen when
* it is automatically closed. The close method provides this immediate
* release.
*
* <p>
* <B>Note:</B> A ResultSet is automatically closed by the Statement the
* Statement that generated it when that Statement is closed, re-executed,
* or is used to retrieve the next result from a sequence of multiple
* results. A ResultSet is also automatically closed when it is garbage
* collected.
* </p>
*
* @exception java.sql.SQLException if a database access error occurs
*/
public synchronized void close() throws java.sql.SQLException {
super.close();
}
/**
* JDBC 2.0 Delete the current row from the result set and the underlying
* database. Cannot be called when on the insert row.
*
* @exception SQLException if a database-access error occurs, or if called
* when on the insert row.
* @throws SQLException if the ResultSet is not updatable or some other
* error occurs
*/
public synchronized void deleteRow() throws SQLException {
if (!isUpdatable) {
throw new NotUpdatable();
}
if (onInsertRow) {
throw new SQLException(
"Can not call deleteRow() when on insert row");
} else if (rowData.size() == 0) {
throw new SQLException("Can't deleteRow() on empty result set");
} else if (isBeforeFirst()) {
throw new SQLException(
"Before start of result set. Can not call deleteRow().");
} else if (isAfterLast()) {
throw new SQLException(
"After end of result set. Can not call deleteRow().");
}
if (deleter == null) {
if (deleteSQL == null) {
generateStatements();
}
deleter = (com.mysql.jdbc.PreparedStatement) connection
.prepareStatement(deleteSQL);
if (deleter.getMaxRows() != 0) {
deleter.setMaxRows(0);
}
}
deleter.clearParameters();
String characterEncoding = null;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -