⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preparedstatement_base.java

📁 Java写的TDS协议(JDBC/ODBC)实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *  SQL BIT value when it sends it to the database.
     *
     *@param  parameterIndex    the first parameter is 1, the second is 2, ...
     *@param  x                 the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setBoolean( int parameterIndex, boolean x ) throws SQLException
    {
        setParam( parameterIndex, new Boolean( x ), java.sql.Types.BIT, -1 );
    }


    /**
     *  Set a parameter to a Java byte value. The driver converts this to a SQL
     *  TINYINT value when it sends it to the database.
     *
     *@param  index    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 index, byte x ) throws SQLException
    {
        setParam( index, new Integer( x ), java.sql.Types.SMALLINT, -1 );
    }


    /**
     *  Set a parameter to a Java array of bytes. The driver converts this to a
     *  SQL VARBINARY or LONGVARBINARY (depending on the argument's size
     *  relative to the driver's limits on VARBINARYs) when it sends it to the
     *  database.
     *
     *@param  parameterIndex    the first parameter is 1, the second is 2, ...
     *@param  x                 the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setBytes(int parameterIndex, byte x[]) throws SQLException
    {
        // when this method creates the parameter the formal type should
        // be a varbinary if the length of 'x' is <=255, image if length>255.
        if( x==null || x.length<=255 || (x.length<=8000 &&
            ((TdsConnection)getConnection()).getTdsVer()==Tds.TDS70) )
            setParam( parameterIndex, x, java.sql.Types.VARBINARY, -1 );
        else
            setParam( parameterIndex, x, java.sql.Types.LONGVARBINARY, -1 );
    }


    /**
     *  Set a parameter to a java.sql.Date value. The driver converts this to a
     *  SQL DATE value when it sends it to the database.
     *
     *@param  parameterIndex    the first parameter is 1, the second is 2, ...
     *@param  value             the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setDate( int parameterIndex, java.sql.Date value )
             throws SQLException
    {
        setParam( parameterIndex, value, java.sql.Types.DATE, -1 );
    }


    /**
     *  Set a parameter to a Java double value. The driver converts this to a
     *  SQL DOUBLE value when it sends it to the database.
     *
     *@param  parameterIndex    the first parameter is 1, the second is 2, ...
     *@param  value             the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setDouble( int parameterIndex, double value ) throws SQLException
    {
        setParam( parameterIndex, new Double( value ), java.sql.Types.DOUBLE, -1 );
    }


    /**
     *  Set a parameter to a Java float value. The driver converts this to a SQL
     *  FLOAT value when it sends it to the database.
     *
     *@param  parameterIndex    the first parameter is 1, the second is 2, ...
     *@param  value             the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setFloat( int parameterIndex, float value ) throws SQLException
    {
        setParam( parameterIndex, new Float( value ), java.sql.Types.REAL, -1 );
    }


    /**
     *  Set a parameter to a Java int value. The driver converts this to a SQL
     *  INTEGER value when it sends it to the database.
     *
     *@param  index    the first parameter is 1, the second is 2, ...
     *@param  value    the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setInt( int index, int value ) throws SQLException
    {
        setParam( index, new Integer( value ), java.sql.Types.INTEGER, -1 );
    }


    /**
     *  Set a parameter to a Java long value. The driver converts this to a SQL
     *  BIGINT value when it sends it to the database.
     *
     *@param  parameterIndex    the first parameter is 1, the second is 2, ...
     *@param  value             the parameter value
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setLong( int parameterIndex, long value ) throws SQLException
    {
        if ( value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE ) {
            setParam( parameterIndex, new Integer( ( int ) value ), java.sql.Types.INTEGER, -1 );
        }
        else {
            setParam( parameterIndex, new Long( value ), java.sql.Types.NUMERIC, 0 );
        }
    }


    /**
     *  Set a parameter to SQL NULL. <P>
     *
     *  <B>Note:</B> You must specify the parameter's SQL type.
     *
     *@param  index    the first parameter is 1, the second is 2, ...
     *@param  type     SQL type code defined by java.sql.Types
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setNull( int index, int type ) throws SQLException
    {
        setParam( index, null, type, -1 );
    }


    /**
     *  <p>
     *
     *  Set the value of a parameter using an object; use the java.lang
     *  equivalent objects for integral values. <p>
     *
     *  The JDBC specification specifies a standard mapping from Java Object
     *  types to SQL types. The given argument java object will be converted to
     *  the corresponding SQL type before being sent to the database. <p>
     *
     *  Note that this method may be used to pass datatabase specific abstract
     *  data types, by using a Driver specific Java type.
     *
     *@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.
     */
    public void setObject( int parameterIndex, Object x ) throws SQLException
    {
        if ( x == null ) {
            setNull( parameterIndex, java.sql.Types.VARCHAR );
        }
        else
                if ( x instanceof java.lang.String ) {
            setString( parameterIndex, ( String ) x );
        }
        else
                if ( x instanceof java.math.BigDecimal ) {
            setBigDecimal( parameterIndex, ( BigDecimal ) x );
        }
        else
                if ( x instanceof java.lang.Integer ) {
            setInt( parameterIndex, ( ( Number ) x ).intValue() );
        }
        else
                if ( x instanceof java.lang.Long ) {
            setLong( parameterIndex, ( ( Number ) x ).longValue() );
        }
        else
                if ( x instanceof java.lang.Byte ) {
            setByte( parameterIndex, ( ( Number ) x ).byteValue() );
        }
        else
                if ( x instanceof java.lang.Short ) {
            setShort( parameterIndex, ( ( Number ) x ).shortValue() );
        }
        else
                if ( x instanceof java.lang.Boolean ) {
            setBoolean( parameterIndex, ( ( Boolean ) x ).booleanValue() );
        }
        else
                if ( x instanceof java.lang.Double ) {
            setDouble( parameterIndex, ( ( Number ) x ).doubleValue() );
        }
        else
                if ( x instanceof java.lang.Float ) {
            setFloat( parameterIndex, ( ( Number ) x ).floatValue() );
        }
        else
                if ( x instanceof java.sql.Date ) {
            setDate( parameterIndex, ( java.sql.Date ) x );
        }
        else
                if ( x instanceof java.util.Date ) {
            setTimestamp( parameterIndex, new Timestamp( ( ( java.util.Date ) x ).getTime() ) );
        }
        else {
            Class c = x.getClass();
            if ( c.isArray() && c.getComponentType().equals( byte.class ) ) {
                setBytes( parameterIndex, ( byte[] ) x );
            }
            else {
                throw new SQLException( "Not implemented" );
            }
        }
    }


    /**
     *  This method is like setObject above, but assumes a scale of zero.
     *
     *@exception  SQLException  if a database-access error occurs.
     */
    public void setObject( int parameterIndex, Object x, int targetSqlType ) throws SQLException
    {
        setObject( parameterIndex, x, targetSqlType, 0 );
    }


    /**
     *  initialize one element in the parameter list
     *
     *@param  index  (in-only) index (first column is 1) of the parameter
     *@param  value  (in-only)
     *@param  type   (in-only) JDBC type
     */
    protected void setParam(
            int index,
            Object value,
            int type,
            int strLength )
             throws SQLException
    {
        if ( index < 1 ) {
            throw new SQLException( "Invalid Parameter index "
                     + index + ".  JDBC indexes start at 1." );
        }
        if ( index > parameterList.length ) {
            throw new SQLException( "Invalid Parameter index "
                     + index + ".  This statement only has "
                     + parameterList.length + " parameters" );
        }

        // JDBC indexes start at 1, java array indexes start at 0 :-(
        index--;

        parameterList[index].type = type;
        parameterList[index].isSet = true;
        parameterList[index].value = value;

        parameterList[index].maxLength = strLength;
    }
    // setParam()


    protected static Map getTypemap()
    {
        if ( typemap != null ) {
            return typemap;
        }

        Map map = new java.util.HashMap( 15 );
        map.put( BigDecimal.class, new Integer( java.sql.Types.NUMERIC ) );
        map.put( Boolean.class, new Integer( java.sql.Types.BIT ) );
        map.put( Byte.class, new Integer( java.sql.Types.TINYINT ) );
        map.put( byte[].class, new Integer( java.sql.Types.VARBINARY ) );
        map.put( java.sql.Date.class, new Integer( java.sql.Types.DATE ) );
        map.put( Double.class, new Integer( java.sql.Types.DOUBLE ) );
        map.put( float.class, new Integer( java.sql.Types.REAL ) );
        map.put( Integer.class, new Integer( java.sql.Types.INTEGER ) );
        map.put( Long.class, new Integer( java.sql.Types.NUMERIC ) );
        map.put( Short.class, new Integer( java.sql.Types.SMALLINT ) );
        map.put( String.class, new Integer( java.sql.Types.VARCHAR ) );
        map.put( java.sql.Timestamp.class, new Integer( java.sql.Types.TIMESTAMP ) );

        typemap = map;
        return typemap;
    }


    protected static int getType( Object o ) throws SQLException
    {
        if ( o == null ) {
            throw new SQLException( "You must specify a type for a null parameter" );
        }

        Map map = getTypemap();
        Object ot = map.get( o.getClass() );
        if ( ot == null ) {
            throw new SQLException( "Support for this type is not implemented" );
        }
        return ( ( Integer ) ot ).intValue();
    }


    //----------------------------------------------------------------------
    // Advanced features:

    /**
     *  <p>
     *
     *  Set the value of a parameter using an object; use the java.lang
     *  equivalent objects for integral values. <p>
     *
     *  The given Java object will be converted to the targetSqlType before
     *  being sent to the database. <p>
     *
     *  Note that this method may be used to pass datatabase- specific abstract
     *  data types. This is done by using a Driver- specific Java type and using
     *  a targetSqlType of java.sql.types.OTHER.
     *
     *@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. For all other types this value will be ignored,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -