📄 statement.java
字号:
* @exception java.sql.SQLException if a database access error occurs
* (why?)
*/
public synchronized void clearWarnings() throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = new Object[0];
Debug.methodCall(this, "clearWarnings", args);
}
this.warningChain = this.pendingWarnings;
this.pendingWarnings = null;
}
/**
* In many cases, it is desirable to immediately release a Statement's
* 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 Statement is automatically closed when it is garbage
* collected. When a Statement is closed, its current ResultSet, if one
* exists, is also closed.
* </p>
*
* @exception java.sql.SQLException if a database access error occurs
*/
public synchronized void close() throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = new Object[0];
Debug.methodCall(this, "close", args);
}
if (this.isClosed) {
return;
}
if (results != null) {
try {
results.close();
} catch (Exception ex) {
;
}
}
if (this.maxRowsChanged && this.connection != null) {
this.connection.unsetMaxRows(this);
}
this.results = null;
this.connection = null;
this.warningChain = null;
this.isClosed = true;
this.closeAllOpenResults();
this.openResults = null;
}
/**
* Execute a SQL statement that may return multiple results. We don't have
* to worry about this since we do not support multiple ResultSets. You
* can use getResultSet or getUpdateCount to retrieve the result.
*
* @param sql any SQL statement
*
* @return true if the next result is a ResulSet, false if it is an update
* count or there are no more results
*
* @exception SQLException if a database access error occurs
*/
public synchronized boolean execute(String sql) throws SQLException {
if (Driver.TRACE) {
Object[] args = { sql };
Debug.methodCall(this, "execute", args);
}
char firstNonWsChar = StringUtils.firstNonWsCharUc(sql);
if (connection.isReadOnly()) {
if (firstNonWsChar != 'S') {
throw new SQLException("Connection is read-only. "
+ "Queries leading to data modification are not allowed",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
}
checkClosed();
if (this.doEscapeProcessing) {
sql = EscapeProcessor.escapeSQL(sql, this.serverSupportsConvertFn);
}
if (results != null) {
results.close();
}
ResultSet rs = 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 (connection.getMutex()) {
clearWarnings();
String oldCatalog = null;
if (!connection.getCatalog().equals(currentCatalog)) {
oldCatalog = connection.getCatalog();
connection.setCatalog(currentCatalog);
}
boolean isSelect = (firstNonWsChar == 'S');
//
// Only apply max_rows to selects
//
if (connection.useMaxRows()) {
int rowLimit = -1;
if (isSelect) {
if (sql.toUpperCase().indexOf("LIMIT") != -1) {
rowLimit = this.maxRows;
} else {
if (maxRows <= 0) {
connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT",
-1, this.currentCatalog);
} else {
connection.execSQL("SET OPTION SQL_SELECT_LIMIT="
+ maxRows, -1, this.currentCatalog);
}
}
} else {
connection.execSQL("SET OPTION SQL_SELECT_LIMIT=DEFAULT",
-1, this.currentCatalog);
}
// Finally, execute the query
rs = connection.execSQL(sql, rowLimit, resultSetConcurrency,
createStreamingResultSet(), isSelect,
this.currentCatalog);
} else {
rs = connection.execSQL(sql, -1, resultSetConcurrency,
createStreamingResultSet(), isSelect,
this.currentCatalog);
}
if (oldCatalog != null) {
connection.setCatalog(oldCatalog);
}
}
lastInsertId = rs.getUpdateID();
if (rs != null) {
this.results = rs;
}
rs.setFirstCharOfQuery(firstNonWsChar);
rs.setConnection(connection);
rs.setResultSetType(resultSetType);
rs.setResultSetConcurrency(resultSetConcurrency);
return ((rs != null) && rs.reallyResult());
}
/**
* @see Statement#execute(String, int)
*/
public boolean execute(String sql, int returnGeneratedKeys)
throws SQLException {
if (returnGeneratedKeys == 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);
}
}
} else {
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);
}
}
} else {
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);
}
}
} else {
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
*/
public synchronized int[] executeBatch() 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);
}
try {
clearWarnings();
int[] updateCounts = null;
if (batchedArgs != null) {
int nbrCommands = 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) batchedArgs
.get(commandIndex), false);
} catch (SQLException ex) {
updateCounts[commandIndex] = EXECUTE_FAILED;
if (this.connection.continueBatchOnError()) {
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 {
if (Driver.TRACE) {
Object[] args = { sql };
Debug.methodCall(this, "executeQuery", args);
}
checkClosed();
if (this.doEscapeProcessing) {
sql = EscapeProcessor.escapeSQL(sql, this.serverSupportsConvertFn);
}
char firstStatementChar = StringUtils.firstNonWsCharUc(sql);
if ((firstStatementChar == 'I') || (firstStatementChar == 'U')
|| (firstStatementChar == 'D') || (firstStatementChar == 'A')
|| (firstStatementChar == 'C')) {
if (StringUtils.startsWithIgnoreCaseAndWs(sql, "INSERT")
|| StringUtils.startsWithIgnoreCaseAndWs(sql, "UPDATE")
|| StringUtils.startsWithIgnoreCaseAndWs(sql, "DELETE")
|| StringUtils.startsWithIgnoreCaseAndWs(sql, "DROP")
|| StringUtils.startsWithIgnoreCaseAndWs(sql, "CREATE")
|| StringUtils.startsWithIgnoreCaseAndWs(sql, "ALTER")) {
throw new SQLException("Can not issue data manipulation statements with executeQuery()",
SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
}
if (results != null) {
results.close();
}
// 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 (connection.getMutex()) {
clearWarnings();
String oldCatalog = null;
if (!connection.getCatalog().equals(currentCatalog)) {
oldCatalog = connection.getCatalog();
connection.setCatalog(currentCatalog);
}
if (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 (sql.toUpperCase().indexOf("LIMIT") != -1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -