📄 jdbcpreparedstatement.java
字号:
/*
* jdbcPreparedStatement.java
*
* Copyright (c) 2001, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This package is based on HypersonicSQL, originally developed by Thomas Mueller.
*
*/
package org.hsqldb;
import java.sql.*;
import java.math.BigDecimal;
import java.io.*;
import java.util.*;
/**
* <P><font color="#009900">
* In HSQL Database Engine, the class jdbcPreparedStatement implements both the
* PreparedStatement and the CallableStatement interfaces.
* <P>
* CallableStatement is used to execute SQL stored procedures,<BR>
* PreparedStatement is used to execute SQL statement with parameters.
* <P>
* There are currently no stored procedures in HSQL Database Engine.
* The reason is that stored procedures are currently extremely
* proprietary. If a stored procedure was made for one database type,
* it will not work in a database of another vendor.
* <P>
* The functions for working with OUT parameters are not implemented
* because there are no statements that returns OUT parameters. All
* statements in HSQL Database Engine returns a ResultSet or a Update Count.
* <P>
* For other databases it may be required to use OUT parameters
* when calling stored procedures or vendor specific statements.
* </font><P>
* A PreparedStatement represents a precompiled SQL statement.
* <P>A SQL statement is pre-compiled and stored in a
* PreparedStatement object. This object can then be used to
* execute this statement multiple times.
*
* <P><B>Note:</B> The setXXX methods for setting IN parameter values
* must specify types that are compatible with the defined SQL type of
* the input parameter. For instance, if the IN parameter has SQL type
* Integer, then the method <code>setInt</code> should be used.
*
* <p>If arbitrary parameter type conversions are required, the method
* <code>setObject</code> should be used with a target SQL type.
* <br>
* Example of setting a parameter; <code>con</code> is an active connection
* <pre><code>
* PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
* SET SALARY = ? WHERE ID = ?");
* pstmt.setBigDecimal(1, 153833.00)
* pstmt.setInt(2, 110592)
* </code></pre>
*
* <P>
* The interface CallableStatement is used to execute SQL
* stored procedures. JDBC provides a stored procedure
* SQL escape that allows stored procedures to be called in a standard
* way for all RDBMSs. This escape syntax has one form that includes
* a result parameter and one that does not. If used, the result
* parameter must be registered as an OUT parameter. The other parameters
* can be used for input, output or both. Parameters are referred to
* sequentially, by number. The first parameter is 1.
* <P>
* <blockquote><pre>
* {?= call <procedure-name>[<arg1>,<arg2>, ...]}
* {call <procedure-name>[<arg1>,<arg2>, ...]}
* </pre></blockquote>
* <P>
* IN parameter values are set using the set methods inherited from
* {@link PreparedStatement}. The type of all OUT parameters must be
* registered prior to executing the stored procedure; their values
* are retrieved after execution via the <code>get</code> methods provided here.
* <P>
* A <code>CallableStatement</code> can return one {@link ResultSet} or
* multiple <code>ResultSet</code> objets. Multiple
* <code>ResultSet</code> objects are handled using operations
* inherited from {@link Statement}.
* <P>
* For maximum portability, a call's <code>ResultSet</code> objects and
* update counts should be processed prior to getting the values of output
* parameters.
*
* @see jdbcResultSet
*/
public class jdbcPreparedStatement extends jdbcStatement
implements PreparedStatement, CallableStatement {
private String sSql;
private Vector vParameter;
/**
* Executes the SQL query in this <code>PreparedStatement</code> object
* and returns the result set generated by the query.
*
* @return a ResultSet that contains the data produced by the
* query; never null
* @exception SQLException if a database access error occurs
*/
public ResultSet executeQuery() throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
return super.executeQuery(build());
}
/**
* Executes the SQL INSERT, UPDATE or DELETE statement
* in this <code>PreparedStatement</code> object.
* In addition,
* SQL statements that return nothing, such as SQL DDL statements,
* can be executed.
*
* @return either the row count for INSERT, UPDATE or DELETE statements;
* or 0 for SQL statements that return nothing
* @exception SQLException if a database access error occurs
*/
public int executeUpdate() throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
return super.executeUpdate(build());
}
/**
* Sets the designated parameter to SQL NULL.
*
* <P><B>Note:</B> You must specify the parameter's SQL type.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param sqlType the SQL type code defined in java.sql.Types
* @exception SQLException if a database access error occurs
*/
public void setNull(int parameterIndex, int sqlType) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setNull(parameterIndex);
}
/**
* Sets the designated parameter to a Java boolean value. The driver
* converts this to an SQL BIT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setBoolean(int parameterIndex,
boolean x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, x ? "TRUE" : "FALSE");
}
/**
* Sets the designated parameter to a Java byte value. The driver
* converts this to an SQL TINYINT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setByte(int parameterIndex, byte x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, "" + x);
}
/**
* Sets the designated parameter to a Java short value. The driver
* converts this to an SQL SMALLINT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setShort(int parameterIndex, short x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, "" + x);
}
/**
* Sets the designated parameter to a Java int value. The driver
* converts this to an SQL INTEGER value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setInt(int parameterIndex, int x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, "" + x);
}
/**
* Sets the designated parameter to a Java long value. The driver
* converts this to an SQL BIGINT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setLong(int parameterIndex, long x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, "" + x);
}
/**
* Sets the designated parameter to a Java float value. The driver
* converts this to an SQL FLOAT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setFloat(int parameterIndex, float x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, "" + x);
}
/**
* Sets the designated parameter to a Java double value. The driver
* converts this to an SQL DOUBLE value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setDouble(int parameterIndex, double x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setParameter(parameterIndex, "" + x);
}
/**
* Sets the designated parameter to a java.lang.BigDecimal value.
* The driver converts this to an SQL NUMERIC value when
* it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setBigDecimal(int parameterIndex,
BigDecimal x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setParameter(parameterIndex, x.toString());
}
}
/**
* Sets the designated parameter to a Java String value. The driver
* converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the
* argument's size relative to the driver's limits on VARCHARs) when it sends
* it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setString(int parameterIndex, String x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setParameter(parameterIndex, Column.createString(x));
}
}
/**
* Sets the designated parameter to a Java array of bytes. The driver
* converts this to an SQL VARBINARY or LONGVARBINARY (depending on the
* argument's size relative to the driver's limits on VARBINARYs)
* when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setParameter(parameterIndex,
"'" + ByteArray.createString(x) + "'");
}
}
/**
* Sets the designated parameter to a java.sql.Date value. The driver
* converts this to an SQL DATE value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setDate(int parameterIndex,
java.sql.Date x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setParameter(parameterIndex, "'" + x.toString() + "'");
}
}
/**
* Sets the designated parameter to a java.sql.Time value. The driver
* converts this to an SQL TIME value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setTime(int parameterIndex, Time x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setParameter(parameterIndex, "'" + x.toString() + "'");
}
}
/**
* Sets the designated parameter to a java.sql.Timestamp value. The driver
* converts this to an SQL TIMESTAMP value when it sends it to the
* database.
*
* @param parameterIndex the first parameter is 1, the second is 2, ...
* @param x the parameter value
* @exception SQLException if a database access error occurs
*/
public void setTimestamp(int parameterIndex,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -