All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drizzle.jdbc.DrizzlePreparedStatement Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
/*
 * Drizzle-JDBC
 *
 * Copyright (c) 2009-2011, Marcus Eriksson, Trond Norbye, Stephane Giron
 *
 * 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 driver 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 COPYRIGHT HOLDER 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.drizzle.jdbc;

import org.drizzle.jdbc.internal.SQLExceptionMapper;
import org.drizzle.jdbc.internal.common.ParameterizedBatchHandler;
import org.drizzle.jdbc.internal.common.Protocol;
import org.drizzle.jdbc.internal.common.QueryException;
import org.drizzle.jdbc.internal.common.Utils;
import org.drizzle.jdbc.internal.common.query.IllegalParameterException;
import org.drizzle.jdbc.internal.common.query.ParameterizedQuery;
import org.drizzle.jdbc.internal.common.query.QueryFactory;
import org.drizzle.jdbc.internal.common.query.parameters.BigDecimalParameter;
import org.drizzle.jdbc.internal.common.query.parameters.BlobStreamingParameter;
import org.drizzle.jdbc.internal.common.query.parameters.BufferedReaderParameter;
import org.drizzle.jdbc.internal.common.query.parameters.BufferedStreamParameter;
import org.drizzle.jdbc.internal.common.query.parameters.ByteParameter;
import org.drizzle.jdbc.internal.common.query.parameters.DateParameter;
import org.drizzle.jdbc.internal.common.query.parameters.DoubleParameter;
import org.drizzle.jdbc.internal.common.query.parameters.IntParameter;
import org.drizzle.jdbc.internal.common.query.parameters.LongParameter;
import org.drizzle.jdbc.internal.common.query.parameters.NullParameter;
import org.drizzle.jdbc.internal.common.query.parameters.ParameterHolder;
import org.drizzle.jdbc.internal.common.query.parameters.ReaderParameter;
import org.drizzle.jdbc.internal.common.query.parameters.SerializableParameter;
import org.drizzle.jdbc.internal.common.query.parameters.StreamParameter;
import org.drizzle.jdbc.internal.common.query.parameters.StringParameter;
import org.drizzle.jdbc.internal.common.query.parameters.TimeParameter;
import org.drizzle.jdbc.internal.common.query.parameters.TimestampParameter;
import org.drizzle.jdbc.internal.common.queryresults.ModifyQueryResult;
import org.drizzle.jdbc.internal.common.queryresults.ResultSetType;

import java.io.IOException;
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;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * User: marcuse Date: Jan 27, 2009 Time: 10:49:42 PM
 */
public class DrizzlePreparedStatement extends DrizzleStatement implements PreparedStatement {
    private final static Logger log = Logger.getLogger(DrizzlePreparedStatement.class.getName());
    private ParameterizedQuery dQuery;
    private final ParameterizedBatchHandler parameterizedBatchHandler;

    public DrizzlePreparedStatement(final Protocol protocol,
                                    final DrizzleConnection drizzleConnection,
                                    final String query,
                                    final QueryFactory queryFactory,
                                    final ParameterizedBatchHandler parameterizedBatchHandler) {
        super(protocol, drizzleConnection, queryFactory);

        if(log.isLoggable(Level.FINEST)) {
            log.finest("Creating prepared statement for " + query);
        }
        dQuery = queryFactory.createParameterizedQuery(query);
        this.parameterizedBatchHandler = parameterizedBatchHandler;
    }

    public ResultSet executeQuery() throws SQLException {
        startTimer();
        try {
            setQueryResult(getProtocol().executeQuery(dQuery));
            return new DrizzleResultSet(getQueryResult(), this, getProtocol());
        } catch (QueryException e) {
            throw SQLExceptionMapper.get(e);
        } finally {
            stopTimer();
        }
    }

    /**
     * Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation
     * Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL
     * statement that returns nothing, such as a DDL statement.
     *
     * @return either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements
     *         that return nothing
     * @throws java.sql.SQLException if a database access error occurs; this method is called on a closed
     *                               PreparedStatement or the SQL statement returns a
     *                               ResultSet object
     */
    public int executeUpdate() throws SQLException {
        startTimer();
        try {
            setQueryResult(getProtocol().executeQuery(dQuery));
            dQuery.clearParameters();
        } catch (QueryException e) {
            throw SQLExceptionMapper.get(e);
        } finally {
            stopTimer();
        }
        if (getQueryResult().getResultSetType() != ResultSetType.MODIFY) {
            throw SQLExceptionMapper.getSQLException("The query returned a result set");
        }
        return (int) ((ModifyQueryResult) getQueryResult()).getUpdateCount();
    }


    /**
     * Sets the designated parameter to SQL NULL.
     * 

*

Note: 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if sqlType is a ARRAY, BLOB, * CLOB, DATALINK, JAVA_OBJECT, * NCHAR, NCLOB, NVARCHAR, * LONGNVARCHAR, REF, ROWID, * SQLXML or STRUCT data type and the JDBC driver does not * support this data type */ public void setNull(final int parameterIndex, final int sqlType) throws SQLException { setParameter(parameterIndex, new NullParameter()); } public boolean execute() throws SQLException { startTimer(); try { setQueryResult(getProtocol().executeQuery(dQuery)); dQuery.clearParameters(); } catch (QueryException e) { throw SQLExceptionMapper.get(e); } finally { stopTimer(); } if (getQueryResult().getResultSetType() == ResultSetType.SELECT) { setResultSet(new DrizzleResultSet(getQueryResult(), this, getProtocol())); return true; } else { setUpdateCount(((ModifyQueryResult) getQueryResult()).getUpdateCount()); return false; } } /** * Adds a set of parameters to this PreparedStatement object's batch of commands. *

*

* * @throws java.sql.SQLException if a database access error occurs or this method is called on a closed * PreparedStatement * @see java.sql.Statement#addBatch * @since 1.2 */ public void addBatch() throws SQLException { parameterizedBatchHandler.addToBatch(dQuery); dQuery = getQueryFactory().createParameterizedQuery(dQuery); } @Override public int[] executeBatch() throws SQLException { try { return parameterizedBatchHandler.executeBatch(); } catch (QueryException e) { throw SQLExceptionMapper.get(e); } } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.2 */ public void setCharacterStream(final int parameterIndex, final Reader reader, final int length) throws SQLException { try { setParameter(parameterIndex, new ReaderParameter(reader, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream: " + e.getMessage(), e); } } /** * 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. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x an SQL REF value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.2 */ public void setRef(final int parameterIndex, final Ref x) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("REF not supported"); } /** * Sets the designated parameter to the given java.sql.Blob object. The driver converts this to an SQL * BLOB value when it sends it to the database. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x a Blob object that maps an SQL BLOB value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.2 */ public void setBlob(final int parameterIndex, final Blob x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new StreamParameter(x.getBinaryStream(), x.length())); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * Sets the designated parameter to the given java.sql.Clob object. The driver converts this to an SQL * CLOB value when it sends it to the database. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x a Clob object that maps an SQL CLOB value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.2 */ public void setClob(final int parameterIndex, final Clob x) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("Clobs not supported"); } /** * Sets the designated parameter to the given java.sql.Array object. The driver converts this to an SQL * ARRAY value when it sends it to the database. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x an Array object that maps an SQL ARRAY value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.2 */ public void setArray(final int parameterIndex, final Array x) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("Arrays not supported"); } /** * 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. * * * @return the description of a ResultSet object's columns or null if the driver cannot * return a ResultSetMetaData object * @throws java.sql.SQLException if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.2 */ public ResultSetMetaData getMetaData() throws SQLException { if (super.getResultSet() != null) { return super.getResultSet().getMetaData(); } return null; } /** * 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 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 date the parameter value * @param cal the Calendar object the driver will use to construct the date * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.2 */ public void setDate(final int parameterIndex, final Date date, final Calendar cal) throws SQLException { if(date == null) { setNull(parameterIndex, Types.DATE); return; } setParameter(parameterIndex, new DateParameter(date.getTime(), cal)); } /** * 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 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 time the parameter value * @param cal the Calendar object the driver will use to construct the time * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.2 */ public void setTime(final int parameterIndex, final Time time, final Calendar cal) throws SQLException { // SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // sdf.setCalendar(cal); if(time == null) { setNull(parameterIndex, Types.TIME); return; } setParameter(parameterIndex, new TimeParameter(time.getTime())); } /** * 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.2 */ public void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException { if(x == null) { setNull(parameterIndex, Types.TIMESTAMP); return; } setParameter(parameterIndex, new TimestampParameter(x.getTime(), cal)); } /** * 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. * * @param parameterIndex 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if sqlType is a ARRAY, BLOB, * CLOB, DATALINK, JAVA_OBJECT, * NCHAR, NCLOB, NVARCHAR, * LONGNVARCHAR, REF, ROWID, * SQLXML or STRUCT data type and the JDBC driver does not * support this data type or if the JDBC driver does not support this method * @since 1.2 */ public void setNull(final int parameterIndex, final int sqlType, final String typeName) throws SQLException { setParameter(parameterIndex, new NullParameter()); } private void setParameter(final int parameterIndex, final ParameterHolder holder) throws SQLException { try { dQuery.setParameter(parameterIndex - 1, holder); } catch (IllegalParameterException e) { throw SQLExceptionMapper.getSQLException("Could not set parameter", e); } } /** * 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. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the java.net.URL object to be set * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.4 */ public void setURL(final int parameterIndex, final URL x) throws SQLException { setParameter(parameterIndex, new StringParameter(x.toString())); } /** * Retrieves the number, types and properties of this PreparedStatement object's parameters. * * @return a ParameterMetaData object that contains information about the number, types and properties * for each parameter marker of this PreparedStatement object * @throws java.sql.SQLException if a database access error occurs or this method is called on a closed * PreparedStatement * @see java.sql.ParameterMetaData * @since 1.4 */ public ParameterMetaData getParameterMetaData() throws SQLException { return new DrizzleParameterMetaData(dQuery); } /** * Sets the designated parameter to the given java.sql.RowId object. The driver converts this to a SQL * ROWID value when it sends it to the database * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setRowId(final int parameterIndex, final java.sql.RowId x) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("RowIDs not supported"); } /** * Sets the designated paramter to the given String object. The driver converts this to a SQL * NCHAR or NVARCHAR or LONGNVARCHAR value (depending on the argument's size * relative to the driver's limits on NVARCHAR values) when it sends it to the database. * * @param parameterIndex of the first parameter is 1, the second is 2, ... * @param value the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if the driver does not support national character sets; if the driver can detect * that a data conversion error could occur; if a database access error occurs; or * this method is called on a closed PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setNString(final int parameterIndex, final String value) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("NStrings not supported"); } /** * Sets the designated parameter to a Reader object. The Reader reads the data till * end-of-file is reached. The driver does the necessary conversion from Java character format to the national * character set in the database. * * @param parameterIndex of the first parameter is 1, the second is 2, ... * @param value the parameter value * @param length the number of characters in the parameter data. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if the driver does not support national character sets; if the driver can detect * that a data conversion error could occur; if a database access error occurs; or * this method is called on a closed PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setNCharacterStream(final int parameterIndex, final Reader value, final long length) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("NCharstreams not supported"); } /** * Sets the designated parameter to a java.sql.NClob object. The driver converts this to a SQL * NCLOB value when it sends it to the database. * * @param parameterIndex of the first parameter is 1, the second is 2, ... * @param value the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if the driver does not support national character sets; if the driver can detect * that a data conversion error could occur; if a database access error occurs; or * this method is called on a closed PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setNClob(final int parameterIndex, final java.sql.NClob value) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("NClobs not supported"); } /** * Sets the designated parameter to a Reader object. The reader must contain the number of characters * specified by length otherwise a SQLException will be generated when the * PreparedStatement is executed. This method differs from the setCharacterStream (int, Reader, * int) method because it informs the driver that the parameter value should be sent to the server as a * CLOB. When the setCharacterStream method is used, the driver may have to do extra work * to determine whether the parameter data should be sent to the server as a LONGVARCHAR or a * CLOB * * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param reader An object that contains the data to set the parameter value to. * @param length the number of characters in the parameter data. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatement or if the length specified is less than zero. * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("Clobs not supported"); } /** * Sets the designated parameter to a InputStream object. The inputstream must contain the number of * characters specified by length otherwise a SQLException will be generated when the * PreparedStatement is executed. This method differs from the setBinaryStream (int, InputStream, * int) method because it informs the driver that the parameter value should be sent to the server as a * BLOB. When the setBinaryStream method is used, the driver may have to do extra work to * determine whether the parameter data should be sent to the server as a LONGVARBINARY or a * BLOB * * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param inputStream An object that contains the data to set the parameter value to. * @param length the number of bytes in the parameter data. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatement; if the length specified is less than zero or if the * number of bytes in the inputstream does not match the specfied length. * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setBlob(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException { if(inputStream == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new StreamParameter(inputStream, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * Sets the designated parameter to a Reader object. The reader must contain the number of characters * specified by length otherwise a SQLException will be generated when the * PreparedStatement is executed. This method differs from the setCharacterStream (int, Reader, * int) method because it informs the driver that the parameter value should be sent to the server as a * NCLOB. When the setCharacterStream method is used, the driver may have to do extra * work to determine whether the parameter data should be sent to the server as a LONGNVARCHAR or a * NCLOB * * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param reader An object that contains the data to set the parameter value to. * @param length the number of characters in the parameter data. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if the length specified is less than zero; if the driver does not support national * character sets; if the driver can detect that a data conversion error could occur; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("NClobs not supported"); } /** * Sets the designated parameter to the given java.sql.SQLXML object. The driver converts this to an * SQL XML value when it sends it to the database. *

* * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param xmlObject a SQLXML object that maps an SQL XML value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatement or the java.xml.transform.Result, * Writer or OutputStream has not been closed for the * SQLXML object * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setSQLXML(final int parameterIndex, final java.sql.SQLXML xmlObject) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("SQlXML not supported"); } /** *

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. *

* If the second argument is an InputStream then the stream must contain the number of bytes specified * by scaleOrLength. If the second argument is a Reader then the reader must contain the number of * characters specified by scaleOrLength. If these conditions are not true the driver will generate a * SQLException when the prepared statement is executed. *

*

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, * NClob, Struct, java.net.URL, 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. * * @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 scaleOrLength for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types, this * is the number of digits after the decimal point. For Java Object types * InputStream and Reader, this is the length of the data in the * stream or reader. For all other types, this value will be ignored. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatement or if the Java Object specified by x is an * InputStream or Reader object and the value of the scale parameter is less than * zero * @throws java.sql.SQLFeatureNotSupportedException * if targetSqlType is a ARRAY, BLOB, * CLOB, DATALINK, JAVA_OBJECT, * NCHAR, NCLOB, NVARCHAR, * LONGNVARCHAR, REF, ROWID, * SQLXML or STRUCT data type and the JDBC driver does not * support this data type * @see java.sql.Types * @since 1.6 */ public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scaleOrLength) throws SQLException { if (x == null) { setNull(parameterIndex, targetSqlType); return; } switch (targetSqlType) { case Types.ARRAY: case Types.CLOB: case Types.DATALINK: case Types.NCHAR: case Types.NCLOB: case Types.NVARCHAR: case Types.LONGNVARCHAR: case Types.REF: case Types.ROWID: case Types.SQLXML: case Types.STRUCT: throw SQLExceptionMapper.getFeatureNotSupportedException("Datatype not supported"); case Types.INTEGER: if (x instanceof Number) { setNumber(parameterIndex, (Number) x); } else { setInt(parameterIndex, Integer.valueOf((String) x)); } } throw SQLExceptionMapper.getFeatureNotSupportedException("Method not yet implemented"); } private void setNumber(final int parameterIndex, final Number number) throws SQLException { if (number instanceof Integer) { setInt(parameterIndex, (Integer) number); } else if (number instanceof Short) { setShort(parameterIndex, (Short) number); } else { setLong(parameterIndex, number.longValue()); } } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.6 */ public void setAsciiStream(final int parameterIndex, final InputStream x, final long length) throws SQLException { if(x == null) { setNull(parameterIndex, Types.VARCHAR); return; } try { setParameter(parameterIndex, new StreamParameter(x, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.6 */ public void setBinaryStream(final int parameterIndex, final InputStream x, final long length) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new StreamParameter(x, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @since 1.6 */ public void setCharacterStream(final int parameterIndex, final Reader reader, final long length) throws SQLException { if(reader == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new ReaderParameter(reader, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream: " + e.getMessage(), e); } } /** * This function reads up the entire stream and stores it in memory since we need to know the length when sending it * to the server use the corresponding method with a length parameter if memory is an issue *

* Sets the designated parameter to the given input stream. 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.

Note: Consult your JDBC driver documentation to determine if it * might be more efficient to use a version of setAsciiStream which takes a length parameter. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the Java input stream that contains the ASCII parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setAsciiStream(final int parameterIndex, final InputStream x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new BufferedStreamParameter(x)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream"); } } /** * This function reads up the entire stream and stores it in memory since we need to know the length when sending it * to the server *

* Sets the designated parameter to the given input stream. 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.

Note: Consult your JDBC driver documentation to determine if it * might be more efficient to use a version of setBinaryStream which takes a length parameter. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the java input stream which contains the binary parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setBinaryStream(final int parameterIndex, final InputStream x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { if(getProtocol().supportsPBMS()) { setParameter(parameterIndex, new BlobStreamingParameter(x, getProtocol().getHost(), "9090", getProtocol().getDatabase())); } else { setParameter(parameterIndex, new BufferedStreamParameter(x)); } } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream"); } } /** * Sets the designated parameter to the given Reader object. 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.

Note: Consult your JDBC driver documentation to determine if it * might be more efficient to use a version of setCharacterStream which takes a length parameter. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param reader the java.io.Reader object that contains the Unicode data * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException { if(reader == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new BufferedReaderParameter(reader)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read reader", e); } } /** * Sets the designated parameter to a Reader object. The Reader reads the data till * end-of-file is reached. The driver does the necessary conversion from Java character format to the national * character set in the database. *

*

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

Note: Consult your JDBC driver documentation to determine if it * might be more efficient to use a version of setNCharacterStream which takes a length parameter. * * @param parameterIndex of the first parameter is 1, the second is 2, ... * @param value the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if the driver does not support national character sets; if the driver can detect * that a data conversion error could occur; if a database access error occurs; or * this method is called on a closed PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setNCharacterStream(final int parameterIndex, final Reader value) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("NChars not supported"); } /** * Sets the designated parameter to a Reader object. This method differs from the * setCharacterStream (int, Reader) method because it informs the driver that the parameter value * should be sent to the server as a CLOB. When the setCharacterStream method is used, * the driver may have to do extra work to determine whether the parameter data should be sent to the server as a * LONGVARCHAR or a CLOB *

*

Note: Consult your JDBC driver documentation to determine if it might be more efficient to use a * version of setClob which takes a length parameter. * * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param reader An object that contains the data to set the parameter value to. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatementor if parameterIndex does not correspond to a * parameter marker in the SQL statement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setClob(final int parameterIndex, final Reader reader) throws SQLException { throw SQLExceptionMapper.getSQLException("CLOBs not supported"); } /** * Sets the designated parameter to a InputStream object. This method differs from the * setBinaryStream (int, InputStream) method because it informs the driver that the parameter value * should be sent to the server as a BLOB. When the setBinaryStream method is used, the * driver may have to do extra work to determine whether the parameter data should be sent to the server as a * LONGVARBINARY or a BLOB *

*

Note: Consult your JDBC driver documentation to determine if it might be more efficient to use a * version of setBlob which takes a length parameter. * * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param inputStream An object that contains the data to set the parameter value to. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatement or if parameterIndex does not correspond to a * parameter marker in the SQL statement, * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException { if(inputStream == null) { setNull(parameterIndex, Types.BLOB); return; } try { if(getProtocol().supportsPBMS()) { setParameter(parameterIndex, new BlobStreamingParameter(inputStream, getProtocol().getHost(), "9090", getProtocol().getDatabase())); } else { setParameter(parameterIndex, new BufferedStreamParameter(inputStream)); } } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream"); } } /** * Sets the designated parameter to a Reader object. This method differs from the * setCharacterStream (int, Reader) method because it informs the driver that the parameter value * should be sent to the server as a NCLOB. When the setCharacterStream method is used, * the driver may have to do extra work to determine whether the parameter data should be sent to the server as a * LONGNVARCHAR or a NCLOB

Note: Consult your JDBC driver documentation to * determine if it might be more efficient to use a version of setNClob which takes a length * parameter. * * @param parameterIndex index of the first parameter is 1, the second is 2, ... * @param reader An object that contains the data to set the parameter value to. * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if the driver does not support national character sets; if the driver can detect * that a data conversion error could occur; if a database access error occurs or * this method is called on a closed PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @since 1.6 */ public void setNClob(final int parameterIndex, final Reader reader) throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("NClobs not supported"); } public void setBoolean(final int column, final boolean value) throws SQLException { if(value) { setByte(column, (byte) 1); } else { setByte(column, (byte) 0); } } /** * 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setByte(final int parameterIndex, final byte x) throws SQLException { setParameter(parameterIndex, new IntParameter(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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setShort(final int parameterIndex, final short x) throws SQLException { setParameter(parameterIndex, new IntParameter(x)); } public void setString(final int column, final String s) throws SQLException { if(s == null) { setNull(column, Types.VARCHAR); return; } setParameter(column, new StringParameter(s)); } /** * 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. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setBytes(final int parameterIndex, final byte[] x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } setParameter(parameterIndex, new ByteParameter(x)); } /** * Sets the designated parameter to the given java.sql.Date value using the default time zone of the * virtual machine that is running the application. 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 date the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setDate(final int parameterIndex, final Date date) throws SQLException { if(date == null) { setNull(parameterIndex, Types.DATE); return; } setParameter(parameterIndex, new DateParameter(date.getTime())); } /** * Since Drizzle has no TIME datatype, time in milliseconds is stored in a packed integer * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @see org.drizzle.jdbc.internal.common.Utils#packTime(long) * @see org.drizzle.jdbc.internal.common.Utils#unpackTime(int) *

* 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. */ public void setTime(final int parameterIndex, final Time x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.TIME); return; } setParameter(parameterIndex, new TimeParameter(x.getTime())); } /** * 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.TIMESTAMP); return; } setParameter(parameterIndex, new TimestampParameter(x.getTime())); } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new StreamParameter(x, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * Sets the designated parameter to the given input stream, which will have the specified number of bytes. *

* 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. *

* The byte format of the Unicode stream must be a Java UTF-8, as defined in the Java Virtual Machine * Specification. *

*

Note: This stream object can either be a standard Java stream object or your own subclass that * implements the standard interface. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x a java.io.InputStream object that contains the Unicode parameter value * @param length the number of bytes in the stream * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if the JDBC driver does not support this method * @deprecated */ public void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new StreamParameter(x, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BLOB); return; } try { setParameter(parameterIndex, new StreamParameter(x, length)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not read stream", e); } } /** * 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. * * @throws java.sql.SQLException if a database access error occurs or this method is called on a closed * PreparedStatement */ public void clearParameters() throws SQLException { dQuery.clearParameters(); } /** * 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. * * @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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement * @throws java.sql.SQLFeatureNotSupportedException * if targetSqlType is a ARRAY, BLOB, * CLOB, DATALINK, JAVA_OBJECT, * NCHAR, NCLOB, NVARCHAR, * LONGNVARCHAR, REF, ROWID, * SQLXML or STRUCT data type and the JDBC driver does not * support this data type * @see java.sql.Types */ public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException { if (Utils.isJava5()) { switch(targetSqlType ) { case Types.ARRAY: case Types.CLOB: case Types.DATALINK: case Types.JAVA_OBJECT: case Types.REF: case Types.STRUCT: throw SQLExceptionMapper.getFeatureNotSupportedException("Type not supported"); } } else { switch(targetSqlType ) { case Types.ARRAY: case Types.CLOB: case Types.DATALINK: case Types.JAVA_OBJECT: case Types.NCHAR: case Types.NCLOB: case Types.NVARCHAR: case Types.LONGNVARCHAR: case Types.REF: case Types.ROWID: case Types.SQLXML: case Types.STRUCT: throw SQLExceptionMapper.getFeatureNotSupportedException("Type not supported"); } } if (x == null) { setNull(parameterIndex,Types.INTEGER); } else if (x instanceof String) { if(targetSqlType == Types.BLOB) { throw SQLExceptionMapper.getSQLException("Cannot convert a String to a Blob"); } String s = (String)x; switch(targetSqlType) { case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: try { setLong(parameterIndex,Long.valueOf(s)); } catch (NumberFormatException e) { throw SQLExceptionMapper.getSQLException("Could not convert ["+s+"] to "+targetSqlType,e); } break; case Types.DOUBLE: try { setDouble(parameterIndex,Double.valueOf(s)); } catch (NumberFormatException e) { throw SQLExceptionMapper.getSQLException("Could not convert ["+s+"] to "+targetSqlType,e); } break; case Types.REAL: case Types.FLOAT: try { setFloat(parameterIndex,Float.valueOf(s)); } catch (NumberFormatException e) { throw SQLExceptionMapper.getSQLException("Could not convert ["+s+"] to "+targetSqlType,e); } break; case Types.DECIMAL: case Types.NUMERIC: try { setBigDecimal(parameterIndex,new BigDecimal(s)); } catch (NumberFormatException e) { throw SQLExceptionMapper.getSQLException("Could not convert ["+s+"] to "+targetSqlType,e); } break; case Types.BIT: setBoolean(parameterIndex, Boolean.valueOf(s)); break; case Types.CHAR: case Types.VARCHAR: case Types.TIMESTAMP: case Types.TIME: setString(parameterIndex, s); break; default: throw SQLExceptionMapper.getSQLException("Could not convert ["+s+"] to "+targetSqlType); } } else if(x instanceof Number) { testNumbers(targetSqlType); Number bd = (Number) x; switch(targetSqlType) { case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: setLong(parameterIndex,bd.longValue()); break; case Types.DOUBLE: setDouble(parameterIndex, bd.doubleValue()); break; case Types.REAL: case Types.FLOAT: setFloat(parameterIndex, bd.floatValue()); break; case Types.DECIMAL: case Types.NUMERIC: if(x instanceof BigDecimal) setBigDecimal(parameterIndex, (BigDecimal)x); else setLong(parameterIndex, bd.longValue()); break; case Types.BIT: setBoolean(parameterIndex, bd.shortValue() != 0); break; case Types.CHAR: case Types.VARCHAR: setString(parameterIndex, bd.toString()); break; default: throw SQLExceptionMapper.getSQLException("Could not convert ["+bd+"] to "+targetSqlType); } } else if (x instanceof byte[]) { if(targetSqlType == Types.BINARY || targetSqlType == Types.VARBINARY || targetSqlType == Types.LONGVARBINARY) { setBytes(parameterIndex, (byte[]) x); } else { throw SQLExceptionMapper.getSQLException("Can only convert a byte[] to BINARY, VARBINARY or LONGVARBINARY"); } } else if (x instanceof Date) { setDate(parameterIndex, (Date) x); // works even if targetSqlType is non date-column } else if (x instanceof Time) { setTime(parameterIndex, (Time) x); // it is just a string anyway } else if (x instanceof Timestamp) { setTimestamp(parameterIndex, (Timestamp) x); } else if (x instanceof Boolean) { testNumbers(targetSqlType); setBoolean(parameterIndex, (Boolean) x); } else if (x instanceof Blob) { setBlob(parameterIndex, (Blob) x); } else { throw SQLExceptionMapper.getSQLException("Could not set parameter in setObject, could not convert: " + x.getClass()+" to "+ targetSqlType); } } private void testNumbers(int targetSqlType) throws SQLException { switch(targetSqlType ) { case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: case Types.DATE: case Types.TIME: case Types.TIMESTAMP: case Types.BLOB: throw SQLExceptionMapper.getSQLException("Cannot convert to "+targetSqlType); } } /** *

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, NClob, * Struct, java.net.URL, RowId, SQLXML or Array, * the driver should pass it to the database as a value of the corresponding SQL type. *

* Note: Not all databases allow for a non-typed Null to be sent to the backend. For maximum portability, the * setNull or the setObject(int parameterIndex, Object x, int sqlType) method should be * used instead of setObject(int parameterIndex, Object x). *

* Note: 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. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the object containing the input parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs; this method is called on a closed * PreparedStatement or the type of the given object is ambiguous */ public void setObject(final int parameterIndex, final Object x) throws SQLException { if (x == null) { setNull(parameterIndex,Types.INTEGER); } else if (x instanceof String) { setString(parameterIndex, (String) x); } else if (x instanceof Integer) { setInt(parameterIndex, (Integer) x); } else if (x instanceof Long) { setLong(parameterIndex, (Long) x); } else if (x instanceof Short) { setShort(parameterIndex, (Short) x); } else if (x instanceof Double) { setDouble(parameterIndex, (Double) x); } else if (x instanceof Float) { setFloat(parameterIndex, (Float) x); } else if (x instanceof Byte) { setByte(parameterIndex, (Byte) x); } else if (x instanceof byte[]) { setBytes(parameterIndex, (byte[]) x); } else if (x instanceof Date) { setDate(parameterIndex, (Date) x); } else if (x instanceof Time) { setTime(parameterIndex, (Time) x); } else if (x instanceof Timestamp) { setTimestamp(parameterIndex, (Timestamp) x); } else if (x instanceof java.util.Date) { setTimestamp(parameterIndex, new Timestamp(((java.util.Date) x).getTime())); } else if (x instanceof Boolean) { setBoolean(parameterIndex, (Boolean) x); } else if (x instanceof Blob) { setBlob(parameterIndex, (Blob) x); } else if (x instanceof InputStream) { setBinaryStream(parameterIndex, (InputStream) x); } else if (x instanceof Reader) { setCharacterStream(parameterIndex, (Reader) x); } else if (x instanceof BigDecimal) { setBigDecimal(parameterIndex, (BigDecimal)x); } else { try { setParameter(parameterIndex, new SerializableParameter(x)); } catch (IOException e) { throw SQLExceptionMapper.getSQLException("Could not set serializable parameter in setObject: " + e.getMessage(), e); } } } public void setInt(final int column, final int i) throws SQLException { setParameter(column, new IntParameter(i)); } /** * 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setLong(final int parameterIndex, final long x) throws SQLException { setParameter(parameterIndex, new LongParameter(x)); } /** * Sets the designated parameter to the given Java float value. The driver converts this to an SQL * REAL value when it sends it to the database. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setFloat(final int parameterIndex, final float x) throws SQLException { setParameter(parameterIndex, new DoubleParameter(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. * * @param parameterIndex the first parameter is 1, the second is 2, ... * @param x the parameter value * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setDouble(final int parameterIndex, final double x) throws SQLException { setParameter(parameterIndex, new DoubleParameter(x)); } /** * 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 * @throws java.sql.SQLException if parameterIndex does not correspond to a parameter marker in the SQL statement; * if a database access error occurs or this method is called on a closed * PreparedStatement */ public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException { if(x == null) { setNull(parameterIndex, Types.BIGINT); return; } setParameter(parameterIndex, new BigDecimalParameter(x)); } /** * Indicates whether the statement is auto-closed when no result sets are * remain open. * * @return True or false. */ public boolean isCloseOnCompletion() throws SQLException { return false; } public void closeOnCompletion() throws SQLException { throw SQLExceptionMapper.getFeatureNotSupportedException("Statement closeOnCompletion"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy