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

📄 preparedstatement.java

📁 SSD9 练习9
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public void setLong(int parameterIndex, long x) throws java.sql.SQLException    {	set(parameterIndex, (new Long(x)).toString());    }    /**     * 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...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setFloat(int parameterIndex, float x) throws java.sql.SQLException    {	set(parameterIndex, (new Float(x)).toString());    }    /**     * 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...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setDouble(int parameterIndex, double x) throws java.sql.SQLException    {	set(parameterIndex, _DoubleFormatter.format(x));	// - Fix for large doubles by Steve Ferguson    }    /**     * Set a parameter to a java.lang.BigDecimal value.  The driver     * converts this to a SQL NUMERIC value when it sends it to the     * database.     *     * @param parameterIndex the first parameter is 1...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setBigDecimal(int parameterIndex, BigDecimal X) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.DECIMAL);	}	else {	    set(parameterIndex, X.toString());	}    }    /**     * Set a parameter to a Java String value.  The driver converts this     * to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments     * size relative to the driver's limits on VARCHARs) when it sends it     * to the database.     *     * @param parameterIndex the first parameter is 1...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setString(int parameterIndex, String X) throws java.sql.SQLException    {	// if the passed string is null, then set this column to null			if(X == null) {	    set(parameterIndex, "null");	}	else {	    StringBuffer B = new StringBuffer();	    int i;			        	    B.append('\'');	    for (i = 0 ; i < X.length() ; ++i) {		char c = X.charAt(i);						if (c == '\\' || c == '\'' || c == '"') {		    B.append((char)'\\');		}		B.append(c);	    }				    B.append('\'');	    set(parameterIndex, B.toString());	}    }    /**     * 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...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setBytes(int parameterIndex, byte x[]) throws java.sql.SQLException    {	if (x == null) {	    setNull(parameterIndex, java.sql.Types.BINARY);	}	else {	    ByteArrayInputStream BIn = new ByteArrayInputStream(x);	    setBinaryStream(parameterIndex, BIn, x.length);	}    }    /**     * 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...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setDate(int parameterIndex, java.sql.Date X) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.DATE);	}	else {	    SimpleDateFormat DF = new SimpleDateFormat("''yyyy-MM-dd''");	    	    set(parameterIndex, DF.format(X));	}    }    /**     * Set a parameter to a java.sql.Time value.  The driver converts     * this to a SQL TIME value when it sends it to the database.     *     * @param parameterIndex the first parameter is 1...));     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setTime(int parameterIndex, Time X) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.TIME);	}	else {	    set(parameterIndex, "'" + X.toString() + "'");	}    }    /**     * Set a parameter to a java.sql.Timestamp value.  The driver converts     * this to a SQL TIMESTAMP value when it sends it to the database.     *     * @param parameterIndex the first parameter is 1...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setTimestamp(int parameterIndex, Timestamp X) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.TIMESTAMP);	}	else {	    EscapeProcessor EP = new EscapeProcessor();	    String TimestampString = EP.escapeSQL("{ts '" + X.toString() + "'}");	    set(parameterIndex, TimestampString);	}    }    /**     * 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.     * JDBC will read the data from the stream as needed, until it reaches     * end-of-file.  The JDBC driver will do any necessary conversion from     * ASCII to the database char format.     *     * <P><B>Note:</B> 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...     * @param x the parameter value     * @param length the number of bytes in the stream     * @exception java.sql.SQLException if a database access error occurs     */    public void setAsciiStream(int parameterIndex, InputStream X, int length) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.VARCHAR);	}	else {	    setBinaryStream(parameterIndex, X, length);	}    }    /**     * 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.     * JDBC will read the data from the stream as needed, until it reaches     * end-of-file.  The JDBC driver will do any necessary conversion from     * UNICODE to the database char format.     *     * <P><B>Note:</B> 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...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setUnicodeStream(int parameterIndex, InputStream X, int length) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.VARCHAR);	}	else {	    setBinaryStream(parameterIndex, X, length);	}    }    /**     * 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.     * JDBC will read the data from the stream as needed, until it reaches     * end-of-file.       *     * <P><B>Note:</B> 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...     * @param x the parameter value     * @exception java.sql.SQLException if a database access error occurs     */    public void setBinaryStream(int parameterIndex, InputStream X, int length) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.BINARY);	}	else {	    if (parameterIndex < 1 || 		parameterIndex > _TemplateStrings.length) {		throw new java.sql.SQLException("Parameter index out of range (" + parameterIndex + " > " + _TemplateStrings.length + ")", "S1009");	    }	    _ParameterStreams[parameterIndex - 1] = X;	    _IsStream[parameterIndex - 1] = true;	}    }    /**     * In general, parameter values remain in force for repeated used of a     * Statement.  Setting a parameter value automatically clears its     * previous value.  However, in coms cases, it is useful to immediately     * release the resources used by the current parameter values; this     * can be done by calling clearParameters     *     * @exception java.sql.SQLException if a database access error occurs     */    public void clearParameters() throws java.sql.SQLException    {	for (int i = 0 ; i < _ParameterStrings.length ; i++) {	    _ParameterStrings[i] = null;	    _ParameterStreams[i] = null;	    _IsStream[i] = false;	}    }    /**     * 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 database-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...     * @param x the object containing the input parameter value     * @param targetSqlType The SQL type to be send to the database     * @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.     * @exception java.sql.SQLException if a database access error occurs     */    public void setObject(int parameterIndex, Object X, int targetSqlType, int scale) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.OTHER);	}	else try {	    switch (targetSqlType)		{		case Types.TINYINT:		case Types.SMALLINT:		case Types.INTEGER:		case Types.BIGINT:		case Types.REAL:		case Types.FLOAT:		case Types.DOUBLE:		case Types.DECIMAL:		case Types.NUMERIC:                    Number X_as_number;		    if (X instanceof Boolean)			X_as_number=((Boolean)X).booleanValue() ? new Integer(1) : new Integer(0);		    else if (X instanceof String)                        switch (targetSqlType) {			case Types.TINYINT:			case Types.SMALLINT:			case Types.INTEGER:			    X_as_number=Integer.valueOf((String)X);			    break;			case Types.BIGINT:			    X_as_number=Long.valueOf((String)X);			    break;			case Types.REAL:			    X_as_number=Float.valueOf((String)X);			    break;			case Types.FLOAT:			case Types.DOUBLE:			    X_as_number=Double.valueOf((String)X);			    break;			case Types.DECIMAL:			case Types.NUMERIC:			default:			    X_as_number=new java.math.BigDecimal((String)X);			}		    else			X_as_number=(Number)X;                    switch (targetSqlType) {		    case Types.TINYINT:		    case Types.SMALLINT:		    case Types.INTEGER:			setInt(parameterIndex, X_as_number.intValue());			break;		    case Types.BIGINT:			setLong(parameterIndex, X_as_number.longValue());			break;		    case Types.REAL:			setFloat(parameterIndex, X_as_number.floatValue());			break;		    case Types.FLOAT:		    case Types.DOUBLE:			setDouble(parameterIndex, X_as_number.doubleValue());			break;		    case Types.DECIMAL:		    case Types.NUMERIC:		    default:			if (X_as_number instanceof java.math.BigDecimal)			    setBigDecimal(parameterIndex, (java.math.BigDecimal)X_as_number);			else if (X_as_number instanceof java.math.BigInteger)			    setBigDecimal(parameterIndex, new java.math.BigDecimal((java.math.BigInteger)X_as_number,scale));			else			    setBigDecimal(parameterIndex, new java.math.BigDecimal(X_as_number.doubleValue()));			break;		    }		    break;		case Types.CHAR:		case Types.VARCHAR:		case Types.LONGVARCHAR:		    setString(parameterIndex, X.toString());		    break;		case Types.BINARY:		case Types.VARBINARY:		case Types.LONGVARBINARY:		    if (X instanceof String)			setBytes(parameterIndex, ((String)X).getBytes());		    else			setBytes(parameterIndex, (byte[])X);		    break;		case Types.DATE:		case Types.TIMESTAMP:                    java.util.Date X_as_date;		    if (X instanceof String) {                        ParsePosition pp=new ParsePosition(0);			java.text.DateFormat sdf=new java.text.SimpleDateFormat(getDateTimePattern((String)X,false));                        X_as_date=sdf.parse((String)X,pp);		    } else			X_as_date=(java.util.Date)X;		    switch(targetSqlType) {		    case Types.DATE:			if (X_as_date instanceof java.sql.Date)			    setDate(parameterIndex,(java.sql.Date)X_as_date);			else			    setDate(parameterIndex,new java.sql.Date(X_as_date.getTime()));			break;		    case Types.TIMESTAMP:			if (X_as_date instanceof java.sql.Timestamp)			    setTimestamp(parameterIndex,(java.sql.Timestamp)X_as_date);			else			    setTimestamp(parameterIndex,new java.sql.Timestamp(X_as_date.getTime()));			break;		    }		    break;		case Types.TIME:		    if (X instanceof String) {			java.text.DateFormat sdf=new java.text.SimpleDateFormat(getDateTimePattern((String)X,true));			setTime(parameterIndex,new java.sql.Time(sdf.parse((String)X).getTime()));		    } else			setTime(parameterIndex,(java.sql.Time)X);		    break;		case Types.OTHER:		    try {			ByteArrayOutputStream BytesOut = new ByteArrayOutputStream();			ObjectOutputStream ObjectOut = new ObjectOutputStream(BytesOut);			ObjectOut.writeObject(X);			ObjectOut.flush();			ObjectOut.close();			BytesOut.flush();			BytesOut.close();      			byte[] buf = BytesOut.toByteArray();			ByteArrayInputStream BytesIn = new ByteArrayInputStream(buf);			setBinaryStream(parameterIndex, BytesIn, -1);		    }		    catch (Exception E) {			throw new java.sql.SQLException("Invalid argument value: " + E.getClass().getName(), "S1009");		    }		    break;		default:		    throw new java.sql.SQLException("Unknown Types value", "S1000");		}        } catch (Exception ex) {	    if (ex instanceof java.sql.SQLException) throw (java.sql.SQLException)ex;	    else throw new java.sql.SQLException("Cannot convert "+X.getClass().toString()+" to SQL type requested", "S1000");	}

⌨️ 快捷键说明

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