📄 jdbcstatement.java
字号:
/**
* Sets the maximum number of bytes for a result set column.
* This method does currently do nothing for this driver.
*
* @param max the maximum size - ignored
* @throws SQLException if this object is closed
*/
public void setMaxFieldSize(int max) throws SQLException {
try {
debugCodeCall("setMaxFieldSize", max);
checkClosed();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Enables or disables processing or JDBC escape syntax.
* See also Connection.nativeSQL.
*
* @param enable - true (default) or false (no conversion is attempted)
* @throws SQLException if this object is closed
*/
public void setEscapeProcessing(boolean enable) throws SQLException {
try {
if (debug()) {
debugCode("setEscapeProcessing("+enable+");");
}
checkClosed();
escapeProcessing = enable;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* [Partially supported] Cancels a currently running statement.
* This method must be called from within another
* thread than the execute method.
* This method is not supported in the server mode.
*
* @throws SQLException if this object is closed
*/
public void cancel() throws SQLException {
try {
debugCodeCall("cancel");
checkClosed();
// executingCommand can be reset by another thread
CommandInterface c = executingCommand;
try {
if (c != null) {
c.cancel();
}
} finally {
setExecutingStatement(null);
}
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the current query timeout in seconds.
* This method will return 0 if no query timeout is set.
* The result is rounded to the next second.
*
* @return the timeout in seconds
* @throws SQLException if this object is closed
*/
public int getQueryTimeout() throws SQLException {
try {
debugCodeCall("getQueryTimeout");
checkClosed();
return conn.getQueryTimeout();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the current query timeout in seconds. Calling this method will
* commit an open transaction, even if the value is the same as before.
* Changing the value will affect all statements of this connection.
*
* @param seconds the timeout in seconds - 0 means no timeout, values
* smaller 0 will throw an exception
* @throws SQLException if this object is closed
*/
public void setQueryTimeout(int seconds) throws SQLException {
try {
debugCodeCall("setQueryTimeout", seconds);
checkClosed();
if (seconds < 0) {
throw Message.getInvalidValueException("" + seconds, "seconds");
}
conn.setQueryTimeout(seconds);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Adds a statement to the batch.
*/
public void addBatch(String sql) throws SQLException {
try {
debugCodeCall("addBatch", sql);
checkClosed();
if (escapeProcessing) {
sql = conn.translateSQL(sql);
}
if (batchCommands == null) {
batchCommands = new ObjectArray();
}
batchCommands.add(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Clears the batch.
*/
public void clearBatch() throws SQLException {
try {
debugCodeCall("clearBatch");
checkClosed();
batchCommands = null;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes the batch.
*
* @return the array of update counts
*/
public int[] executeBatch() throws SQLException {
try {
debugCodeCall("executeBatch");
checkClosed();
if (batchCommands == null) {
// TODO batch: check what other database do if no commands are set
batchCommands = new ObjectArray();
}
int[] result = new int[batchCommands.size()];
boolean error = false;
for (int i = 0; i < batchCommands.size(); i++) {
String sql = (String) batchCommands.get(i);
try {
result[i] = executeUpdate(sql);
} catch (SQLException e) {
logAndConvert(e);
//#ifdef JDK14
result[i] = Statement.EXECUTE_FAILED;
//#endif
error = true;
}
}
batchCommands = null;
if (error) {
throw new BatchUpdateException(result);
}
return result;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Return a result set that contains the last generated autoincrement key
* for this connection.
*
* @return the result set with one row and one column containing the key
* @throws SQLException if this object is closed
*/
public ResultSet getGeneratedKeys() throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (debug()) {
debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "getGeneratedKeys()");
}
checkClosed();
ResultInterface result = conn.getGeneratedKeys(this, id);
ResultSet rs = new JdbcResultSet(session, conn, this, result, id, false, true);
return rs;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* [Not supported]
*/
public boolean getMoreResults(int current) throws SQLException {
try {
debugCodeCall("getMoreResults");
throw Message.getUnsupportedException();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement and returns the update count.
* This method just calls executeUpdate(String sql).
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
try {
if (debug()) {
debugCode("executeUpdate("+quote(sql)+", "+autoGeneratedKeys+");");
}
return executeUpdate(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement and returns the update count.
* This method just calls executeUpdate(String sql).
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
try {
if (debug()) {
debugCode("executeUpdate("+quote(sql)+", "+quoteIntArray(columnIndexes)+");");
}
return executeUpdate(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement and returns the update count.
* This method just calls executeUpdate(String sql).
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
try {
if (debug()) {
debugCode("executeUpdate("+quote(sql)+", "+quoteArray(columnNames)+");");
}
return executeUpdate(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement and returns the update count.
* This method just calls execute(String sql).
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
try {
if (debug()) {
debugCode("execute("+quote(sql)+", "+autoGeneratedKeys+");");
}
return execute(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement and returns the update count.
* This method just calls execute(String sql).
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
try {
if (debug()) {
debugCode("execute("+quote(sql)+", "+quoteIntArray(columnIndexes)+");");
}
return execute(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement and returns the update count.
* This method just calls execute(String sql).
*
* @param sql the SQL statement
* @return the update count (number of row affected by an insert,
* update or delete, or 0 if no rows or the statement was a
* create, drop, commit or rollback)
* @throws SQLException if a database error occurred or a
* select statement was executed
*/
public boolean execute(String sql, String[] columnNames) throws SQLException {
try {
if (debug()) {
debugCode("execute("+quote(sql)+", "+quoteArray(columnNames)+");");
}
return execute(sql);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Gets the result set holdability.
*
* @return the holdability
*/
//#ifdef JDK14
public int getResultSetHoldability() throws SQLException {
try {
debugCodeCall("getResultSetHoldability");
checkClosed();
return ResultSet.HOLD_CURSORS_OVER_COMMIT;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
//#endif
// =============================================================
JdbcStatement(SessionInterface session, JdbcConnection conn, int resultSetType, int id, boolean closeWithResultSet) {
setTrace(session.getTrace(), TraceObject.STATEMENT, id);
this.session = session;
this.conn = conn;
this.resultSetType = resultSetType;
this.closedByResultSet = closeWithResultSet;
}
void checkClosed() throws SQLException {
if (conn == null) {
throw Message.getSQLException(ErrorCode.OBJECT_CLOSED);
}
conn.checkClosed();
}
void closeOld() throws SQLException {
try {
if (!closedByResultSet) {
if (resultSet != null) {
resultSet.closeInternal();
}
}
} finally {
resultSet = null;
updateCount = -1;
}
}
protected void setExecutingStatement(CommandInterface c) {
conn.setExecutingStatement(c == null ? null : this);
executingCommand = c;
}
/**
* Returns whether this statement is closed.
*
* @return true if the statement is closed
*/
public boolean isClosed() throws SQLException {
try {
debugCodeCall("isClosed");
return conn == null;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* [Not supported] Return an object of this class if possible.
*/
//#ifdef JDK16
/*
public <T> T unwrap(Class<T> iface) throws SQLException {
throw Message.getUnsupportedException();
}
*/
//#endif
/**
* [Not supported] Checks if unwrap can return an object of this class.
*/
//#ifdef JDK16
/*
public boolean isWrapperFor(Class< ? > iface) throws SQLException {
throw Message.getUnsupportedException();
}
*/
//#endif
/**
* Returns whether this object is poolable.
* @return false
*/
public boolean isPoolable() throws SQLException {
debugCodeCall("isPoolable");
return false;
}
/**
* Requests that this object should be pooled or not.
* This call is ignored.
*
* @param poolable the requested value
*/
public void setPoolable(boolean poolable) throws SQLException {
if (debug()) {
debugCode("setPoolable("+poolable+");");
}
}
/**
* INTERNAL
*/
public String toString() {
return getTraceObjectName();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -