devdaily home | apple | java | perl | unix | directory | blog

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/* Copyright (c) 2001-2004, 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 HSQL DEVELOPMENT GROUP, HSQLDB.ORG, 
 * 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.
 */


package org.hsqldb.jdbc;

import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Calendar;

import org.hsqldb.lib.ArrayUtil;
import org.hsqldb.lib.HsqlByteArrayOutputStream;
import org.hsqldb.lib.Iterator;
import org.hsqldb.lib.StringConverter;
import org.hsqldb.Binary;
import org.hsqldb.Column;
import org.hsqldb.HsqlDateTime;
import org.hsqldb.HsqlException;
import org.hsqldb.JavaObject;
import org.hsqldb.Result;
import org.hsqldb.ResultConstants;
import org.hsqldb.Trace;
import org.hsqldb.Types;

// fredt@users 20020320 - patch 1.7.0 - JDBC 2 support and error trapping
// JDBC 2 methods can now be called from jdk 1.1.x - see javadoc comments
// boucherb@users 20020509 - added "throws SQLException" to all methods where
// it was missing here but specified in the java.sql.PreparedStatement and
// java.sqlCallableStatement interfaces, updated generic documentation to
// JDK 1.4, and added JDBC3 methods and docs
// boucherb@users and fredt@users 20020409/20020505 extensive review and update
// of docs and behaviour to comply with previous and latest java.sql specification
// fredt@users 20030620 - patch 1.7.2 - rewritten to support real prepared statements
// boucherb@users 20030801 - patch 1.7.2 - support for batch execution
// boucherb@users 20030801 - patch 1.7.2 - support for getMetaData and getParameterMetadata
// boucherb@users 20030801 - patch 1.7.2 - updated some setXXX methods
// boucherb@users 200403/4xx - doc 1.7.2 - javadoc updates toward 1.7.2 final
// boucherb@users 200403/4xx - patch 1.7.2 - eliminate eager buffer allocation from setXXXStream/Blob/Clob

/**
 * 
 *
 * An object that represents a precompiled SQL statement. 

* * An SQL statement is precompiled and stored in a * PreparedStatement object. This object can then be used to * efficiently execute this statement multiple times. * *

Note: The setter methods (setShort, * setString, and so on) 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 setInt should be * used.

* * If arbitrary parameter type conversions are required, the method * setObject should be used with a target SQL type. *

* In the following example of setting a parameter, con * represents an active connection: *

 * PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
 *                               SET SALARY = ? WHERE ID = ?");
 * pstmt.setBigDecimal(1, 153833.00)
 * pstmt.setInt(2, 110592)
 * 

* * *

*

HSQLDB-Specific Information:

* * Starting with HSQLDB 1.7.2, jdbcPreparedStatement objects are backed by * a true compiled parameteric representation. Hence, there are now significant * performance gains to be had by using a jdbcPreparedStatement object in * preference to a jdbcStatement object, if a short-running SQL statement is * to be executed more than a small number of times.

* * Please note, however, that 1.7.2 does not yet provide a sophisticated * internal statement pooling facility. For this reason, the observation * above is guaranteed to apply only under certain use patterns.

* * Specifically, when it can be otherwise avoided, it is to be considered poor * practice to fully prepare (construct), parameterize, execute, fetch and * close a jdbcPreparedStatement object for each execution cycle. Indeed, under * HSQLDB 1.7.2, this practice is likely to be noticably less * performant for short-running statements than the equivalent process using * jdbcStatement objects, albeit far more convenient, less error prone and * certainly much less resource-intensive, especially when large binary and * character values are involved, due to the optimized parameterization * facility.

* * Instead, when developing an application that is not totally oriented toward * the execution of ad hoc SQL, it is recommended to expend some effort toward * identifing the SQL statements that are good candidates for regular reuse and * adapting the structure of the application accordingly. Often, this is done * by recording the text of candidate SQL statements in an application resource * object (which has the nice side-benefit of isolating and hiding differences * in SQL dialects across different drivers) and caching for possible reuse the * PreparedStatement objects derived from the recorded text.

* * Multi thread use:

* * A PreparedStatement object is stateful and should not normally be shared * by multiple threads. If it has to be shared, the calls to set the * parameters, calls to add batch statements, the execute call and any * post-execute calls should be made within a block synchronized on the * PreparedStatement Object.

* * JRE 1.1.x Notes:

* * In general, JDBC 2 support requires Java 1.2 and above, and JDBC3 requires * Java 1.4 and above. In HSQLDB, support for methods introduced in different * versions of JDBC depends on the JDK version used for compiling and building * HSQLDB.

* * Since 1.7.0, it is possible to build the product so that * all JDBC 2 methods can be called while executing under the version 1.1.x * Java Runtime EnvironmentTM. * However, in addition to requiring explicit casts to the org.hsqldb.jdbcXXX * interface implementations, some of these method calls require * int values that are defined only in the JDBC 2 or greater * version of * * ResultSet interface. For this reason, when the * product is compiled under JDK 1.1.x, these values are defined in * {@link jdbcResultSet jdbcResultSet}.

* * In a JRE 1.1.x environment, calling JDBC 2 methods that take or return the * JDBC2-only ResultSet values can be achieved by referring * to them in parameter specifications and return value comparisons, * respectively, as follows:

* *

 * jdbcResultSet.FETCH_FORWARD
 * jdbcResultSet.TYPE_FORWARD_ONLY
 * jdbcResultSet.TYPE_SCROLL_INSENSITIVE
 * jdbcResultSet.CONCUR_READ_ONLY
 * // etc.
 * 
* * However, please note that code written in such a manner will not be * compatible for use with other JDBC 2 drivers, since they expect and use * ResultSet, rather than jdbcResultSet. Also * note, this feature is offered solely as a convenience to developers * who must work under JDK 1.1.x due to operating constraints, yet wish to * use some of the more advanced features available under the JDBC 2 * specification.

* * (fredt@users)
* (boucherb@users) *

* * * @author boucherb@users * @author fredt@users * @version 1.7.2 * @see jdbcConnection#prepareStatement * @see jdbcResultSet */ public class jdbcPreparedStatement extends jdbcStatement implements java.sql.PreparedStatement { /** The parameter values for the next non-batch execution. */ protected Object[] parameterValues; /** The SQL types of the parameters. */ protected int[] parameterTypes; /** The (IN, IN OUT, or OUT) modes of parameters */ protected int[] parameterModes; /** Lengths for streams. */ protected int[] streamLengths; /** Has a stream or CLOB / BLOB parameter value. */ protected boolean hasStreams; /** * Description of result set metadata.

*/ protected Result rsmdDescriptor; /** Description of parameter metadata. */ protected Result pmdDescriptor; /** This object's one and one ResultSetMetaData object. */ protected jdbcResultSetMetaData rsmd; // NOTE: pmd is declared as Object to avoid yet another #ifdef. /** This object's one and only ParameterMetaData object. */ protected Object pmd; /** The SQL character sequence that this object represents. */ protected String sql; /** * The id with which this object's corresponding * {@link org.hsqldb.CompiledStatement CompiledStatement} * object is registered in the engine's * {@link org.hsqldb.CompiledStatementManager CompiledStatementManager} * object. */ protected int statementID; /** * Whether this statement generates only a single row update count in * response to execution. */ protected boolean isRowCount; // fredt@users 20020215 - patch 517028 by peterhudson@users - method defined // fredt@users 20020215 - patch 517028 by peterhudson@users - method defined // // changes by fredt // SimpleDateFormat objects moved out of methods to improve performance // this is safe because only one thread at a time should access a // PreparedStatement object until it has finished executing the statement // fredt@users 20020215 - patch 517028 by peterhudson@users - method defined // minor changes by fredt /** * * Sets escape processing on or off.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.0, the implementation follows the standard * behaviour by overriding the same method in jdbcStatement * class.

* * In other words, calling this method has no effect. *

* * * @param enable true to enable escape processing; * false to disable it * @exception SQLException if a database access error occurs */ public void setEscapeProcessing(boolean enable) throws SQLException { checkClosed(); } /** * * Executes the SQL statement in this PreparedStatement * object, which may be any kind of SQL statement. * Some prepared statements return multiple results; the * execute method handles these complex statements as well * as the simpler form of statements handled by the methods * executeQueryand executeUpdate.

* * The execute method returns a boolean to * indicate the form of the first result. You must call either the method * getResultSet or getUpdateCount * to retrieve the result; you must call getMoreResults to * move to any subsequent result(s).

* * * *

*

HSQLDB-Specific Information:

* * Including 1.7.2, prepared statements do not generate * multiple fetchable results.

* * Following 1.7.2, it will be possible that statements * generate multiple fetchable results under certain conditions. *

* * @return true if the first result is a ResultSet * object; false if the first result is an update * count or there is no result * @exception SQLException if a database access error occurs or an argument * is supplied to this method * @see jdbcStatement#execute * @see jdbcStatement#getResultSet * @see jdbcStatement#getUpdateCount * @see jdbcStatement#getMoreResults */ public boolean execute() throws SQLException { checkClosed(); connection.clearWarningsNoCheck(); resultIn = null; try { resultOut.setMaxRows(maxRows); resultOut.setParameterData(parameterValues); resultIn = connection.sessionProxy.execute(resultOut); } catch (HsqlException e) { throw jdbcUtil.sqlException(e); } if (resultIn.iMode == ResultConstants.ERROR) { jdbcUtil.throwError(resultIn); } return resultIn.iMode == ResultConstants.DATA ? true : false; } /** * * Executes the SQL query in this PreparedStatement object * and returns the ResultSet object generated by the query.

* * * @return a ResultSet object that contains the data produced * by the query; never null * @exception SQLException if a database access error occurs or the SQL * statement does not return a ResultSet object */ public ResultSet executeQuery() throws SQLException { checkClosed(); connection.clearWarningsNoCheck(); checkIsRowCount(false); resultIn = null; try { resultOut.setMaxRows(maxRows); resultOut.setParameterData(parameterValues); resultIn = connection.sessionProxy.execute(resultOut); } catch (HsqlException e) { throw jdbcUtil.sqlException(e); } if (resultIn.iMode == ResultConstants.ERROR) { jdbcUtil.throwError(resultIn); } else if (resultIn.iMode != ResultConstants.DATA) { String msg = "Expected but did not recieve a result set"; throw jdbcUtil.sqlException(Trace.UNEXPECTED_EXCEPTION, msg); } return new jdbcResultSet(this, resultIn, connection.connProperties, connection.isNetConn); } /** * * Executes the SQL statement in this PreparedStatement * object, which must be an SQL INSERT, * UPDATE or DELETE statement; or an SQL * statement that returns nothing, such as a DDL statement.

* * * @return either (1) the row count for INSERT, * UPDATE, or DELETE * statements or (2) 0 for SQL statements that * return nothing * @exception SQLException if a database access error occurs or the SQL * statement returns a ResultSet object */ public int executeUpdate() throws SQLException { checkClosed(); connection.clearWarningsNoCheck(); checkIsRowCount(true); resultIn = null; try { resultOut.setParameterData(parameterValues); resultIn = connection.sessionProxy.execute(resultOut); } catch (HsqlException e) { throw jdbcUtil.sqlException(e); } if (resultIn.iMode == ResultConstants.ERROR) { jdbcUtil.throwError(resultIn); } else if (resultIn.iMode != ResultConstants.UPDATECOUNT) { String msg = "Expected but did not recieve a row update count"; throw jdbcUtil.sqlException(Trace.UNEXPECTED_EXCEPTION, msg); } return resultIn.getUpdateCount(); } /** * * Submits a batch of commands to the database for execution and * if all commands execute successfully, returns an array of update counts. * The int elements of the array that is returned are ordered * to correspond to the commands in the batch, which are ordered * according to the order in which they were added to the batch. * The elements in the array returned by the method executeBatch * may be one of the following: *

    *
  1. A number greater than or equal to zero -- indicates that the * command was processed successfully and is an update count giving the * number of rows in the database that were affected by the command's * execution *
  2. A value of SUCCESS_NO_INFO -- indicates that the command was * processed successfully but that the number of rows affected is * unknown *

    * If one of the commands in a batch update fails to execute properly, * this method throws a BatchUpdateException, and a JDBC * driver may or may not continue to process the remaining commands in * the batch. However, the driver's behavior must be consistent with a * particular DBMS, either always continuing to process commands or never * continuing to process commands. If the driver continues processing * after a failure, the array returned by the method * BatchUpdateException.getUpdateCounts * will contain as many elements as there are commands in the batch, and * at least one of the elements will be the following: *

    *

  3. A value of EXECUTE_FAILED -- indicates that the command failed * to execute successfully and occurs only if a driver continues to * process commands after a command fails *
*

* A driver is not required to implement this method. * The possible implementations and return values have been modified in * the Java 2 SDK, Standard Edition, version 1.3 to * accommodate the option of continuing to proccess commands in a batch * update after a BatchUpdateException obejct has been thrown.

* * * *

*

HSQLDB-Specific Information:

* * Starting with HSQLDB 1.7.2, this feature is supported.

* * HSQLDB stops execution of commands in a batch when one of the commands * results in an exception. The size of the returned array equals the * number of commands that were executed successfully.

* * When the product is built under the JAVA1 target, an exception * is never thrown and it is the responsibility of the client software to * check the size of the returned update count array to determine if any * batch items failed. To build and run under the JAVA2 target, JDK/JRE * 1.3 or higher must be used. *

* * * @return an array of update counts containing one element for each * command in the batch. The elements of the array are ordered according * to the order in which commands were added to the batch. * @exception SQLException if a database access error occurs or the * driver does not support batch statements. Throws * {@link java.sql.BatchUpdateException} * (a subclass of java.sql.SQLException) if one of the commands * sent to the database fails to execute properly or attempts to return a * result set. * @since JDK 1.3 (JDK 1.1.x developers: read the new overview * for jdbcStatement) */ public int[] executeBatch() throws SQLException { if (batchResultOut == null) { batchResultOut = new Result(ResultConstants.BATCHEXECUTE, parameterTypes, statementID); } return super.executeBatch(); } /** * * Sets the designated parameter to SQL NULL.

* * Note: You must specify the parameter's SQL type.

* * * *

*

HSQLDB-Specific Information:

* * HSQLDB ignores the sqlType argument. *

* * * @param paramIndex 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 paramIndex, int sqlType) throws SQLException { setParameter(paramIndex, null); } /** * * Sets the designated parameter to the given Java boolean * value. The driver converts this to an SQL BIT value * when it sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, HSQLDB uses the BOOLEAN type instead of BIT, as * per SQL 200n (SQL 3). *

* * * @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 { Boolean b = x ? Boolean.TRUE : Boolean.FALSE; setParameter(parameterIndex, b); } /** * * Sets the designated parameter to the given 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 { setIntParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given 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 { setIntParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given 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 { setIntParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given 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 { setLongParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given Java float value. * The driver converts this to an SQL FLOAT value when * it sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.1, HSQLDB handles Java positive/negative Infinity * and NaN float values consistent with the Java Language * Specification; these special values are now correctly stored * to and retrieved from 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 { setDouble(parameterIndex, (double) x); } /** * * Sets the designated parameter to the given Java double value. * The driver converts this to an SQL DOUBLE value when it * sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.1, HSQLDB handles Java positive/negative Infinity * and NaN double values consistent with the Java Language * Specification; these special values are now correctly stored * to and retrieved from 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 { Double d = new Double(x); setParameter(parameterIndex, d); } /** * * Sets the designated parameter to the given * java.math.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 { setParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given 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 VARCHAR values) * when it sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Including 1.7.2, HSQLDB stores all XXXCHAR values as java.lang.String * objects; there is no appreciable difference between * CHAR, VARCHAR and LONGVARCHAR. *

* * * @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 { setParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given 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 VARBINARY values) when it * sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Including 1.7.2, HSQLDB stores all XXXBINARY values the same way; there * is no appreciable difference between BINARY, VARBINARY and * LONGVARBINARY. *

* * * @param paramIndex 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 paramIndex, byte[] x) throws SQLException { setParameter(paramIndex, x); } /** * * Sets the designated parameter to the given * 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 { setParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given 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, java.sql.Time x) throws SQLException { setParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given * 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, java.sql.Timestamp x) throws SQLException { setParameter(parameterIndex, x); } /** * * Sets the designated parameter to the given input stream, which will have * the specified number of bytes. * When a very large ASCII value is input to a LONGVARCHAR * parameter, it may be more practical to send it via a * java.io.InputStream. Data will be read from the stream * as needed until end-of-file is reached. The JDBC driver will * do any necessary conversion from ASCII to the database char format.

* * Note: This stream object can either be a standard * Java stream object or your own subclass that implements the * standard interface.

* * *

*

HSQLDB-Specific Information:

* * This method uses the default platform character encoding to convert bytes * from the stream into the characters of a String. In the future this is * likely to change to always treat the stream as ASCII.

* * Before HSQLDB 1.7.0, setAsciiStream and * setUnicodeStream were identical. *

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the Java input stream that contains the ASCII parameter value * @param length the number of bytes in the stream * @exception SQLException if a database access error occurs */ public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { checkSetParameterIndex(parameterIndex); String s; if (x == null) { s = "input stream is null"; throw jdbcUtil.sqlException(Trace.INVALID_JDBC_ARGUMENT, s); } try { s = StringConverter.inputStreamToString(x, length); setParameter(parameterIndex, s); } catch (IOException e) { throw jdbcUtil.sqlException(Trace.INVALID_CHARACTER_ENCODING); } } /** * * Sets the designated parameter to the given input stream, which * will have the specified number of bytes. A Unicode character has * two bytes, with the first byte being the high byte, and the second * being the low byte. * * When a very large Unicode value is input to a LONGVARCHAR * parameter, it may be more practical to send it via a * java.io.InputStream object. The data will be read from the * stream as needed until end-of-file is reached. The JDBC driver will * do any necessary conversion from Unicode to the database char format. * *

Note: This stream object can either be a standard * Java stream object or your own subclass that implements the * standard interface.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.0, this method complies with behavior as defined by the * JDBC3 specification. *

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x a java.io.InputStream object that contains the * Unicode parameter value as two-byte Unicode characters * @param length the number of bytes in the stream * @exception SQLException if a database access error occurs * @deprecated Sun does not include a reason, but presumably * this is because setCharacterStream is now prefered */ public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { checkSetParameterIndex(parameterIndex); String msg = null; if (x == null) { msg = "input stream is null"; } else if (length % 2 != 0) { msg = "odd length argument"; } if (msg != null) { throw jdbcUtil.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg); } int chlen = length / 2; int chread = 0; StringBuffer sb = new StringBuffer(); int hi; int lo; try { for (; chread < chlen; chread++) { hi = x.read(); if (hi == -1) { break; } lo = x.read(); if (lo == -1) { break; } sb.append((char) (hi << 8 | lo)); } } catch (IOException e) { throw jdbcUtil.sqlException(Trace.TRANSFER_CORRUPTED); } setParameter(parameterIndex, sb.toString()); } /** * * Sets the designated parameter to the given input stream, which will have * the specified number of bytes. * When a very large binary value is input to a LONGVARBINARY * parameter, it may be more practical to send it via a * java.io.InputStream object. The data will be read from the * stream as needed until end-of-file is reached. * *

Note: This stream object can either be a standard * Java stream object or your own subclass that implements the * standard interface.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, this method works according to the standard. *

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the java input stream which contains the binary parameter value * @param length the number of bytes in the stream * @exception SQLException if a database access error occurs */ public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException { checkSetParameterIndex(parameterIndex); if (x == null) { throw jdbcUtil.sqlException( Trace.error( Trace.INVALID_JDBC_ARGUMENT, Trace.JDBC_NULL_STREAM)); } final HsqlByteArrayOutputStream out = new HsqlByteArrayOutputStream(); final int size = 2048; final byte[] buff = new byte[size]; try { for (int left = length; left > 0; ) { final int read = x.read(buff, 0, left > size ? size : left); if (read == -1) { break; } out.write(buff, 0, read); left -= read; } } catch (IOException e) { throw jdbcUtil.sqlException(Trace.INPUTSTREAM_ERROR, e.getMessage()); } setParameter(parameterIndex, out.toByteArray()); } /** * * Clears the current parameter values immediately.

* * In general, parameter values remain in force for repeated use of a * statement. Setting a parameter value automatically clears its * previous value. However, in some cases it is useful to immediately * release the resources used by the current parameter values; this can * be done by calling the method clearParameters.

* * * @exception SQLException if a database access error occurs */ public void clearParameters() throws SQLException { checkClosed(); ArrayUtil.fillArray(parameterValues, null); } //---------------------------------------------------------------------- // Advanced features: /** * * Sets the value of the designated parameter with the given object.

* * The second argument must be an object type; for integral values, the * java.lang equivalent objects should be used.

* * The given Java object will be converted to the given targetSqlType * before being sent to the database. * * If the object has a custom mapping (is of a class implementing the * interface SQLData), * the JDBC driver should call the method SQLData.writeSQL to * write it to the SQL data stream. * If, on the other hand, the object is of a class implementing * Ref, Blob, Clob, * Struct, or Array, the driver should pass it * to the database as a value of the corresponding SQL type.

* * Note that this method may be used to pass database-specific * abstract data types.

* * * *

*

HSQLDB-Specific Information:

* * Inculding 1.7.1,this method was identical to * {@link #setObject(int, Object, int) setObject(int, Object, int)}. * That is, this method simply called setObject(int, Object, int), * ignoring the scale specification.

* * Since 1.7.2, this method supports the conversions listed in the * conversion table B-5 of the JDBC 3 specification. The scale argument * is not used. *

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the object containing the input parameter value * @param targetSqlType the SQL type (as defined in java.sql.Types) to be * sent to the database. The scale argument may further qualify this type. * @param scale for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types, * this is the number of digits after the decimal point. For all * other types, this value will be ignored.

* * Up to and including HSQLDB 1.7.0, this parameter is ignored. * @exception SQLException if a database access error occurs * @see java.sql.Types * @see #setObject(int,Object,int) */ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException { /** @todo fredt - implement SQLData support */ setObject(parameterIndex, x); } /** * * Sets the value of the designated parameter with the given object. * This method is like the method setObject * above, except that it assumes a scale of zero.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, this method supports conversions listed in the * conversion table B-5 of the JDBC 3 specification. *

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the object containing the input parameter value * @param targetSqlType the SQL type (as defined in java.sql.Types) to be * sent to the database * @exception SQLException if a database access error occurs * @see #setObject(int,Object) */ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { setObject(parameterIndex, x); } /** * * Sets the value of the designated parameter using the given object.

* * The second parameter must be of type Object; therefore, * the java.lang equivalent objects should be used for * built-in types.

* * The JDBC specification specifies a standard mapping from * Java Object types to SQL types. The given argument * will be converted to the corresponding SQL type before being * sent to the database.

* * Note that this method may be used to pass datatabase- * specific abstract data types, by using a driver-specific Java * type. If the object is of a class implementing the interface * SQLData, the JDBC driver should call the method * SQLData.writeSQL to write it to the SQL data stream. * If, on the other hand, the object is of a class implementing * Ref, Blob, Clob, * Struct, or Array, the driver should pass * it to the database as a value of the corresponding SQL type.

* * This method throws an exception if there is an ambiguity, for * example, if the object is of a class implementing more than one * of the interfaces named above.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, this method supports conversions listed in the conversion * table B-5 of the JDBC 3 specification.

* *

* * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the object containing the input parameter value * @exception SQLException if a database access error occurs or the type * of the given object is ambiguous */ public void setObject(int parameterIndex, Object x) throws SQLException { setParameter(parameterIndex, x); } //--------------------------JDBC 2.0----------------------------- /** * * Adds a set of parameters to this PreparedStatement * object's batch of commands.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, this feature is supported. *

* * * @exception SQLException if a database access error occurs * @see jdbcStatement#addBatch * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // boucherb@users 20030801 - method implemented public void addBatch() throws SQLException { checkClosed(); int len = parameterValues.length; Object[] bpValues = new Object[len]; System.arraycopy(parameterValues, 0, bpValues, 0, len); if (batchResultOut == null) { batchResultOut = new Result(ResultConstants.BATCHEXECUTE, parameterTypes, statementID); } batchResultOut.add(bpValues); } /** * * Sets the designated parameter to the given Reader * object, which is the given number of characters long. * When a very large UNICODE value is input to a LONGVARCHAR * parameter, it may be more practical to send it via a * java.io.Reader object. The data will be read from the * stream as needed until end-of-file is reached. The JDBC driver will * do any necessary conversion from UNICODE to the database char format. * *

Note: This stream object can either be a standard * Java stream object or your own subclass that implements the * standard interface.

* * * *

*

HSQLDB-Specific Information:

* * HSQLDB stores CHARACTER and related SQL types as Unicode so * this method does not perform any conversion. *

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param reader the java.io.Reader object that contains the * Unicode data * @param length the number of characters in the stream * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // fredt@users 20020429 - patch 1.7.0 - method defined // fredt@users 20020627 - patch 574234 by ohioedge@users // boucherb@users 20030801 - patch 1.7.2 - updated public void setCharacterStream(int parameterIndex, java.io.Reader reader, int length) throws SQLException { checkSetParameterIndex(parameterIndex); if (reader == null) { String msg = "reader is null"; throw jdbcUtil.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg); } final StringBuffer sb = new StringBuffer(); final int size = 2048; final char[] buff = new char[size]; try { for (int left = length; left > 0; ) { final int read = reader.read(buff, 0, left > size ? size : left); if (read == -1) { break; } sb.append(buff, 0, read); left -= read; } } catch (IOException e) { throw jdbcUtil.sqlException(Trace.TRANSFER_CORRUPTED, e.toString()); } setParameter(parameterIndex, sb.toString()); } /** * * Sets the designated parameter to the given * REF(<structured-type>) value. * The driver converts this to an SQL REF value when it * sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * HSQLDB 1.7.2 does not support the SQL REF type. Calling this method * throws an exception. * *

* * @param i the first parameter is 1, the second is 2, ... * @param x an SQL REF value * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ public void setRef(int i, Ref x) throws SQLException { throw jdbcUtil.notSupported; } /** * * Sets the designated parameter to the given Blob object. * The driver converts this to an SQL BLOB value when it * sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Previous to 1.7.2, this feature was not supported.

* * Since 1.7.2, setBlob is supported. With 1.7.2, setting Blob objects is * limited to those of length less than or equal to Integer.MAX_VALUE. * In 1.7.2, setBlob(i,x) is roughly equivalent (null and length handling * not shown) to: * *

     * setBinaryStream(i, x.getBinaryStream(), (int) x.length());
     * 
* * * @param i the first parameter is 1, the second is 2, ... * @param x a Blob object that maps an SQL BLOB * value * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // boucherb@users 20030801 - method implemented public void setBlob(int i, Blob x) throws SQLException { if (x instanceof jdbcBlob) { setParameter(i, ((jdbcBlob) x).data); return; } else if (x == null) { setParameter(i, null); return; } checkSetParameterIndex(i); final long length = x.length(); if (length > Integer.MAX_VALUE) { String msg = "Maximum Blob input octet length exceeded: " + length; throw jdbcUtil.sqlException(Trace.INPUTSTREAM_ERROR, msg); } final java.io.InputStream in = x.getBinaryStream(); final HsqlByteArrayOutputStream out = new HsqlByteArrayOutputStream(); final int buffSize = 2048; final byte[] buff = new byte[buffSize]; try { for (int left = (int) length; left > 0; ) { final int read = in.read(buff, 0, left > buffSize ? buffSize : left); if (read == -1) { break; } out.write(buff, 0, read); left -= read; } } catch (IOException e) { throw jdbcUtil.sqlException(Trace.INPUTSTREAM_ERROR, e.getMessage()); } setParameter(i, out.toByteArray()); } /** * * Sets the designated parameter to the given Clob object. * The driver converts this to an SQL CLOB value when it * sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * Previous to 1.7.2, this feature was not supported.

* * Since 1.7.2, setClob is supported. With 1.7.2, setting Blob objects is * limited to those of length less than or equal to Integer.MAX_VALUE. * In 1.7.2, setClob(i,x) is rougly equivalent (null and length handling * not shown) to:

* *

     * setCharacterStream(i, x.getCharacterStream(), (int) x.length());
     * 
* * @param i the first parameter is 1, the second is 2, ... * @param x a Clob object that maps an SQL CLOB * value * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // boucherb@users 20030801 - method implemented public void setClob(int i, Clob x) throws SQLException { if (x instanceof jdbcClob) { setParameter(i, ((jdbcClob) x).data); return; } else if (x == null) { setParameter(i, null); return; } checkSetParameterIndex(i); final long length = x.length(); if (length > Integer.MAX_VALUE) { String msg = "Max Clob input character length exceeded: " + length; throw jdbcUtil.sqlException(Trace.INPUTSTREAM_ERROR, msg); } java.io.Reader reader = x.getCharacterStream(); final StringBuffer sb = new StringBuffer(); final int size = 2048; final char[] buff = new char[size]; try { for (int left = (int) length; left > 0; ) { final int read = reader.read(buff, 0, left > size ? size : left); if (read == -1) { break; } sb.append(buff, 0, read); left -= read; } } catch (IOException e) { throw jdbcUtil.sqlException(Trace.TRANSFER_CORRUPTED, e.toString()); } setParameter(i, sb.toString()); } /** * * Sets the designated parameter to the given Array object. * The driver converts this to an SQL ARRAY value when it * sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * HSQLDB 1.7.2 does not support the SQL ARRAY type. Calling this method * throws an exception. * *

* * @param i the first parameter is 1, the second is 2, ... * @param x an Array object that maps an SQL ARRAY * value * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ public void setArray(int i, Array x) throws SQLException { throw jdbcUtil.notSupported; } /** * * Retrieves a ResultSetMetaData object that contains * information about the columns of the ResultSet object * that will be returned when this PreparedStatement object * is executed. *

* Because a PreparedStatement object is precompiled, it is * possible to know about the ResultSet object that it will * return without having to execute it. Consequently, it is possible * to invoke the method getMetaData on a * PreparedStatement object rather than waiting to execute * it and then invoking the ResultSet.getMetaData method * on the ResultSet object that is returned. *

* NOTE: Using this method may be expensive for some drivers due * to the lack of underlying DBMS support.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, this feature is supported. If the statement * generates an update count, then null is returned. * *

* * @return the description of a ResultSet object's columns or * null if the driver cannot return a * ResultSetMetaData object * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // boucherb@users 20030801 - method implemented public ResultSetMetaData getMetaData() throws SQLException { checkClosed(); if (isRowCount) { return null; } if (rsmd == null) { rsmd = new jdbcResultSetMetaData(rsmdDescriptor, connection.connProperties); } return rsmd; } /** * * Sets the designated parameter to the given java.sql.Date * value, using the given Calendar object. The driver uses * the Calendar object to construct an SQL DATE * value,which the driver then sends to the database. With a * a Calendar object, the driver can calculate the date * taking into account a custom timezone. If no * Calendar object is specified, the driver uses the default * timezone, which is that of the virtual machine running the * application.

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @param cal the Calendar object the driver will use * to construct the date * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // fredt@users 20020414 - patch 517028 by peterhudson@users - method defined // changes by fredt - moved conversion to HsqlDateTime public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException { String s; try { s = HsqlDateTime.getDateString(x, cal); } catch (Exception e) { throw jdbcUtil.sqlException(Trace.INVALID_ESCAPE, e.getMessage()); } setParameter(parameterIndex, s); } /** * * Sets the designated parameter to the given java.sql.Time * value, using the given Calendar object. The driver uses * the Calendar object to construct an SQL TIME * value, which the driver then sends to the database. With a * a Calendar object, the driver can calculate the time * taking into account a custom timezone. If no * Calendar object is specified, the driver uses the default * timezone, which is that of the virtual machine running the * application.

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @param cal the Calendar object the driver will use * to construct the time * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // fredt@users 20020414 - patch 517028 by peterhudson@users - method defined // changes by fredt - moved conversion to HsqlDateTime public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException { String s; try { s = HsqlDateTime.getTimeString(x, cal); } catch (Exception e) { throw jdbcUtil.sqlException(Trace.INVALID_ESCAPE, e.getMessage()); } setParameter(parameterIndex, s); } /** * * Sets the designated parameter to the given java.sql.Timestamp * value, using the given Calendar object. The driver uses * the Calendar object to construct an SQL TIMESTAMP * value, which the driver then sends to the database. With a * Calendar object, the driver can calculate the timestamp * taking into account a custom timezone. If no * Calendar object is specified, the driver uses the default * timezone, which is that of the virtual machine running the application.

* * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @param cal the Calendar object the driver will use * to construct the timestamp * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ // fredt@users 20020414 - patch 517028 by peterhudson@users - method defined // changes by fredt - moved conversion to HsqlDateTime public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException { checkSetParameterIndex(parameterIndex); String s; try { s = HsqlDateTime.getTimestampString(x, cal); } catch (Exception e) { throw jdbcUtil.sqlException(Trace.INVALID_ESCAPE, e.getMessage()); } setParameter(parameterIndex, s); } /** * * Sets the designated parameter to SQL NULL. * This version of the method setNull should * be used for user-defined types and REF type parameters. Examples * of user-defined types include: STRUCT, DISTINCT, JAVA_OBJECT, and * named array types. * *

Note: To be portable, applications must give the * SQL type code and the fully-qualified SQL type name when specifying * a NULL user-defined or REF parameter. In the case of a user-defined * type the name is the type name of the parameter itself. For a REF * parameter, the name is the type name of the referenced type. If * a JDBC driver does not need the type code or type name information, * it may ignore it. * * Although it is intended for user-defined and Ref parameters, * this method may be used to set a null parameter of any JDBC type. * If the parameter does not have a user-defined or REF type, the given * typeName is ignored.

* * * *

*

HSQLDB-Specific Information:

* * HSQLDB ignores the sqlType and typeName arguments. *

* * * @param paramIndex the first parameter is 1, the second is 2, ... * @param sqlType a value from java.sql.Types * @param typeName the fully-qualified name of an SQL user-defined type; * ignored if the parameter is not a user-defined type or REF * @exception SQLException if a database access error occurs * @since JDK 1.2 (JDK 1.1.x developers: read the new overview for * jdbcPreparedStatement) */ public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException { setParameter(paramIndex, null); } //------------------------- JDBC 3.0 ----------------------------------- /** * * Sets the designated parameter to the given java.net.URL * value. The driver converts this to an SQL DATALINK value * when it sends it to the database.

* * * *

*

HSQLDB-Specific Information:

* * HSQLDB 1.7.2 does not support the DATALINK SQL type for which this * method is intended. Calling this method throws an exception. * *

* * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the java.net.URL object to be set * @exception SQLException if a database access error occurs * @since JDK 1.4, HSQL 1.7.0 */ //#ifdef JDBC3 public void setURL(int parameterIndex, java.net.URL x) throws SQLException { throw jdbcUtil.notSupported; } //#endif JDBC3 /** * * Retrieves the number, types and properties of this * PreparedStatement object's parameters.

* * * *

*

HSQLDB-Specific Information:

* * Since 1.7.2, this feature is supported. *

* * * @return a ParameterMetaData object that contains information * about the number, types and properties of this * PreparedStatement object's parameters * @exception SQLException if a database access error occurs * @see java.sql.ParameterMetaData * @since JDK 1.4, HSQL 1.7.0 */ // boucherb@users 20030801 - method implemented //#ifdef JDBC3 public ParameterMetaData getParameterMetaData() throws SQLException { checkClosed(); if (pmd == null) { pmd = new jdbcParameterMetaData(pmdDescriptor); } // NOTE: pmd is declared as Object to avoid yet another #ifdef. return (ParameterMetaData) pmd; } //#endif JDBC3 //-------------------- Internal Implementation ----------------------------- /** * Constructs a statement that produces results of the requested * type.

* * A prepared statement must be a single SQL statement.

* * @param c the Connection used execute this statement * @param sql the SQL statement this object represents * @param type the type of result this statement will produce * @throws HsqlException if the statement is not accepted by the database * @throws SQLException if preprocessing by driver fails */ jdbcPreparedStatement(jdbcConnection c, String sql, int type) throws HsqlException, SQLException { super(c, type); sql = c.nativeSQL(sql); resultOut.setResultType(ResultConstants.SQLPREPARE); resultOut.setMainString(sql); Result in = connection.sessionProxy.execute(resultOut); if (in.iMode == ResultConstants.ERROR) { jdbcUtil.throwError(in); } // else it's a MULTI result encapsulating three sub results: // 1.) a PREPARE_ACK // // Indicates the statement id to be communicated in SQLEXECUTE // requests to allow the engine to find the corresponding // CompiledStatement object, parameterize and execute it. // // 2.) a description of the statement's result set metadata // // This is communicated in the same way as for result sets. That is, // the metadata arrays of Result, such as colTypes, are used in the // "conventional" fashion. With some work, it may be possible // to speed up internal execution of prepared statements by // dispensing with generating most rsmd values while generating // the result, safe in the knowlege that the client already // has a copy of the rsmd. In general, only the colTypes array // must be available at the engine, and only for network // communications so that the row output and row input // interfaces can do their work. One caveat is that the // columnDisplaySize values are not accurate, as we do // not consistently enforce column size yet and instead // approximate the value when a result with actual data is // retrieved // // 3.) a description of the statement's parameter metadata // // This is communicated in a similar fashion to 2.), but has // a slighly different layout to allow the parameter modes // to be transmitted. The values of this object are used // to set up the parameter management of this class. The // object is also used to construct the jdbcParameterMetaData // object later, if requested. That is, it holds information // additional to that used by this class, so it should not be // altered or disposed of // // (boucherb@users) Iterator i; i = in.iterator(); try { Object[] row; // PREPARE_ACK row = (Object[]) i.next(); statementID = ((Result) row[0]).getStatementID(); // DATA - isParameterDescription == false row = (Object[]) i.next(); rsmdDescriptor = (Result) row[0]; isRowCount = rsmdDescriptor.iMode == ResultConstants.UPDATECOUNT; // DATA - isParameterDescription == true row = (Object[]) i.next(); pmdDescriptor = (Result) row[0]; parameterTypes = pmdDescriptor.metaData.getParameterTypes(); parameterValues = new Object[parameterTypes.length]; parameterModes = pmdDescriptor.metaData.paramMode; } catch (Exception e) { throw Trace.error(Trace.GENERAL_ERROR, e.toString()); } resultOut = new Result(ResultConstants.SQLEXECUTE, parameterTypes, statementID); // for toString() this.sql = sql; } /** * Checks if execution does or does not generate a single row * update count, throwing if the argument, yes, does not match.

* * @param yes if true, check that execution generates a single * row update count, else check that execution generates * something other than a single row update count. * @throws SQLException if the argument, yes, does not match */ protected void checkIsRowCount(boolean yes) throws SQLException { if (yes != isRowCount) { int msg = yes ? Trace.JDBC_STATEMENT_NOT_ROW_COUNT : Trace.JDBC_STATEMENT_NOT_RESULTSET; throw jdbcUtil.sqlException(msg); } } /** * Checks if the specified parameter index value is valid in terms of * setting an IN or IN OUT parameter value.

* * @param i The parameter index to check * @throws SQLException if the specified parameter index is invalid */ protected void checkSetParameterIndex(int i) throws SQLException { int mode; String msg; checkClosed(); if (i < 1 || i > parameterValues.length) { msg = "parameter index out of range: " + i; throw jdbcUtil.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg); } /* mode = parameterModes[i - 1]; switch (mode) { default : msg = "Not IN or IN OUT mode: " + mode + " for parameter: " + i; throw jdbcUtil.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg); case Expression.PARAM_IN : case Expression.PARAM_IN_OUT : break; } */ } /** * The internal parameter value setter always converts the parameter to * the Java type required for data transmission. Target BINARY and OTHER * types are converted directly. All other target types are converted * by Column.convertObject(). This also normalizes DATETIME values. * * @param i parameter index * @param o object * @throws SQLException if either argument is not acceptable. */ private void setParameter(int i, Object o) throws SQLException { checkSetParameterIndex(i); i--; if (o == null) { parameterValues[i] = null; return; } int outType = parameterTypes[i]; try { if (outType == Types.OTHER) { o = new JavaObject((Serializable) o); } else if (outType == Types.BINARY) { if (!(o instanceof byte[])) { throw jdbcUtil.sqlException( Trace.error(Trace.INVALID_CONVERSION)); } o = new Binary((byte[]) o, !connection.isNetConn); } else { Object oldobject = o; o = Column.convertObject(o, outType); // this ensures duplicate objects are stored as internal or ValuePool objects // in order to avoid possible subsequent modifications if (o == oldobject) { if (outType == Types.DATE) { o = HsqlDateTime.getNormalisedDate((java.sql.Date) o); } else if (outType == Types.TIME &&!connection.isNetConn) { o = HsqlDateTime.getNormalisedTime((java.sql.Time) o); } else if (outType == Types.TIMESTAMP &&!connection.isNetConn) { o = ((java.sql.Timestamp) o).clone(); } } } } catch (HsqlException e) { jdbcUtil.throwError(e); } parameterValues[i] = o; } /** * Used with int and narrower integral primitives * @param i parameter index * @param value object to set * @throws SQLException if either argument is not acceptable */ private void setIntParameter(int i, int value) throws SQLException { checkSetParameterIndex(i); int outType = parameterTypes[i - 1]; switch (outType) { case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : Object o = new Integer(value); parameterValues[i - 1] = o; break; default : setLongParameter(i, value); } } /** * Used with long and narrower integral primitives. Conversion to BINARY * or OTHER types will throw here and not passed to setParameter(). * * @param i parameter index * @param value object to set * @throws SQLException if either argument is not acceptable */ private void setLongParameter(int i, long value) throws SQLException { checkSetParameterIndex(i); int outType = parameterTypes[i - 1]; switch (outType) { case Types.BIGINT : Object o = new Long(value); parameterValues[i - 1] = o; break; case Types.BINARY : case Types.OTHER : throw jdbcUtil.sqlException( Trace.error(Trace.INVALID_CONVERSION)); default : setParameter(i, new Long(value)); } } /** * This method should always throw if called for a PreparedStatement or * CallableStatment. * * @param sql ignored * @throws SQLException always */ public void addBatch(String sql) throws java.sql.SQLException { throw jdbcUtil.notSupported; } /** * This method should always throw if called for a PreparedStatement or * CallableStatment. * * @param sql ignored * @throws SQLException always * @return nothing */ public ResultSet executeQuery(String sql) throws java.sql.SQLException { throw jdbcUtil.notSupported; } /** * This method should always throw if called for a PreparedStatement or * CallableStatment. * * @param sql ignored * @throws SQLException always * @return nothing */ public boolean execute(String sql) throws java.sql.SQLException { throw jdbcUtil.notSupported; } /** * This method should always throw if called for a PreparedStatement or * CallableStatment. * * @param sql ignored * @throws SQLException always * @return nothing */ public int executeUpdate(String sql) throws java.sql.SQLException { throw jdbcUtil.notSupported; } /** * Does the specialized work required to free this object's resources and * that of it's parent class.

* * @throws SQLException if a database access error occurs */ public void close() throws java.sql.SQLException { HsqlException he; if (isClosed()) { return; } he = null; try { // fredt - if this is called by Connection.close() then there's no // need to free the prepared statements on the server - it is done // by Connection.close() if (!connection.isClosed) { connection.sessionProxy.execute( Result.newFreeStmtRequest(statementID)); } } catch (HsqlException e) { he = e; } parameterValues = null; parameterTypes = null; parameterModes = null; rsmdDescriptor = null; pmdDescriptor = null; rsmd = null; pmd = null; super.close(); if (he != null) { throw jdbcUtil.sqlException(he); } } /** * Retrieves a String representation of this object.

* * The representation is of the form:

* * class-name@hash[sql=[char-sequence], parameters=[p1, ...pi, ...pn]]

* * p1, ...pi, ...pn are the String representations of the currently set * parameter values that will be used with the non-batch execution * methods.

* * @return a String representation of this object */ public String toString() { StringBuffer sb = new StringBuffer(); String sql; Object[] pv; sb.append(super.toString()); sql = this.sql; pv = parameterValues; if (sql == null || pv == null) { sb.append("[closed]"); return sb.toString(); } sb.append("[sql=[").append(sql).append("]"); if (pv.length > 0) { sb.append(", parameters=["); for (int i = 0; i < pv.length; i++) { sb.append('['); sb.append(pv[i]); sb.append("], "); } sb.setLength(sb.length() - 2); sb.append(']'); } sb.append(']'); return sb.toString(); } }




Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
 
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com