📄 jdbcpreparedstatement.java
字号:
/*
* Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
//#ifdef JDK14
import java.sql.ParameterMetaData;
import java.sql.Statement;
//#endif
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Calendar;
import org.h2.command.CommandInterface;
import org.h2.constant.ErrorCode;
import org.h2.engine.SessionInterface;
import org.h2.expression.ParameterInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
import org.h2.result.ResultInterface;
import org.h2.util.DateTimeUtils;
import org.h2.util.IOUtils;
import org.h2.util.ObjectArray;
import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueByte;
import org.h2.value.ValueBytes;
import org.h2.value.ValueDate;
import org.h2.value.ValueDecimal;
import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat;
import org.h2.value.ValueInt;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull;
import org.h2.value.ValueShort;
import org.h2.value.ValueString;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp;
//#ifdef JDK16
/*
import java.sql.RowId;
import java.sql.NClob;
import java.sql.SQLXML;
*/
//#endif
/**
* Represents a prepared statement.
*
*/
public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement {
private CommandInterface command;
private ObjectArray batchParameters;
/**
* Executes a query (select statement) and returns the result set. If
* another result set exists for this statement, this will be closed (even
* if this statement fails).
*
* @return the result set
* @throws SQLException if this object is closed or invalid
*/
public ResultSet executeQuery() throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (debug()) {
debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "executeQuery()");
}
checkClosed();
closeOld();
ResultInterface result;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
synchronized (session) {
try {
setExecutingStatement(command);
result = command.executeQuery(maxRows, scrollable);
} finally {
setExecutingStatement(null);
}
}
resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet, scrollable);
return resultSet;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Executes a statement (insert, update, delete, create, drop, commit,
* rollback) and returns the update count. If another result set exists for
* this statement, this will be closed (even if this statement fails).
*
* If the statement is a create or drop and does not throw an exception, the
* current transaction (if any) is committed after executing the statement.
* If auto commit is on, this statement will be committed.
*
* @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 this object is closed or invalid
*/
public int executeUpdate() throws SQLException {
try {
debugCodeCall("executeUpdate");
checkClosed();
return executeUpdateInternal();
} catch (Throwable e) {
throw logAndConvert(e);
}
}
private int executeUpdateInternal() throws SQLException {
closeOld();
synchronized (session) {
try {
setExecutingStatement(command);
updateCount = command.executeUpdate();
} finally {
setExecutingStatement(null);
}
}
return updateCount;
}
/**
* Executes an arbitrary statement. If another result set exists for this
* statement, this will be closed (even if this statement fails). If auto
* commit is on, and the statement is not a select, this statement will be
* committed.
*
* @return true if a result set is available, false if not
* @throws SQLException if this object is closed or invalid
*/
public boolean execute() throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (debug()) {
debugCodeCall("execute");
}
checkClosed();
closeOld();
boolean returnsResultSet;
synchronized (session) {
try {
setExecutingStatement(command);
if (command.isQuery()) {
returnsResultSet = true;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
ResultInterface result = command.executeQuery(maxRows, scrollable);
resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet, scrollable);
} else {
returnsResultSet = false;
updateCount = command.executeUpdate();
}
} finally {
setExecutingStatement(null);
}
}
return returnsResultSet;
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Clears all parameters.
*
* @throws SQLException if this object is closed or invalid
*/
public void clearParameters() throws SQLException {
try {
debugCodeCall("clearParameters");
checkClosed();
ObjectArray parameters = command.getParameters();
for (int i = 0; i < parameters.size(); i++) {
ParameterInterface param = (ParameterInterface) parameters.get(i);
param.setValue(null);
}
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Calling this method is not legal on a PreparedStatement.
*
* @throws SQLException Unsupported Feature
*/
public ResultSet executeQuery(String sql) throws SQLException {
try {
debugCodeCall("executeQuery", sql);
throw Message.getSQLException(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Calling this method is not legal on a PreparedStatement.
*
* @throws SQLException Unsupported Feature
*/
public void addBatch(String sql) throws SQLException {
try {
debugCodeCall("addBatch", sql);
throw Message.getSQLException(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Calling this method is not legal on a PreparedStatement.
*
* @throws SQLException Unsupported Feature
*/
public int executeUpdate(String sql) throws SQLException {
try {
debugCodeCall("executeUpdate", sql);
throw Message.getSQLException(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Calling this method is not legal on a PreparedStatement.
*
* @throws SQLException Unsupported Feature
*/
public boolean execute(String sql) throws SQLException {
try {
debugCodeCall("execute", sql);
throw Message.getSQLException(ErrorCode.METHOD_NOT_ALLOWED_FOR_PREPARED_STATEMENT);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
// =============================================================
/**
* Sets a parameter to null.
*
* @param parameterIndex the parameter index (1, 2, ...)
* @param sqlType the data type (Types.x)
* @throws SQLException if this object is closed
*/
public void setNull(int parameterIndex, int sqlType) throws SQLException {
try {
if (debug()) {
debugCode("setNull("+parameterIndex+", "+sqlType+");");
}
setParameter(parameterIndex, ValueNull.INSTANCE);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the value of a parameter.
*
* @param parameterIndex the parameter index (1, 2, ...)
* @param x the value
* @throws SQLException if this object is closed
*/
public void setInt(int parameterIndex, int x) throws SQLException {
try {
if (debug()) {
debugCode("setInt("+parameterIndex+", "+x+");");
}
setParameter(parameterIndex, ValueInt.get(x));
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the value of a parameter.
*
* @param parameterIndex the parameter index (1, 2, ...)
* @param x the value
* @throws SQLException if this object is closed
*/
public void setString(int parameterIndex, String x) throws SQLException {
try {
if (debug()) {
debugCode("setString("+parameterIndex+", "+quote(x)+");");
}
Value v = x == null ? (Value) ValueNull.INSTANCE : ValueString.get(x);
setParameter(parameterIndex, v);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the value of a parameter.
*
* @param parameterIndex the parameter index (1, 2, ...)
* @param x the value
* @throws SQLException if this object is closed
*/
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
try {
if (debug()) {
debugCode("setBigDecimal("+parameterIndex+", " + quoteBigDecimal(x) + ");");
}
Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDecimal.get(x);
setParameter(parameterIndex, v);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the value of a parameter.
*
* @param parameterIndex the parameter index (1, 2, ...)
* @param x the value
* @throws SQLException if this object is closed
*/
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException {
try {
if (debug()) {
debugCode("setDate("+parameterIndex+", " + quoteDate(x) + ");");
}
Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDate.get(x);
setParameter(parameterIndex, v);
} catch (Throwable e) {
throw logAndConvert(e);
}
}
/**
* Sets the value of a parameter.
*
* @param parameterIndex the parameter index (1, 2, ...)
* @param x the value
* @throws SQLException if this object is closed
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -