📄 preparedstatement.java
字号:
}
/**
* Set a parameter to a Java short value. The driver converts this
* to a SQL SMALLINT 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 setShort(int parameterIndex, short x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* 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 parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setInt(int parameterIndex, int x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* 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...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setLong(int parameterIndex, long x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* 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, fixDecimalExponent(String.valueOf(x)));
}
/**
* 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));
set(parameterIndex, fixDecimalExponent(String.valueOf(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, fixDecimalExponent(X.toString()));
}
}
//
// Adds '+' to decimal numbers that are positive (MySQL doesn't
// understand them otherwise
//
protected final static String fixDecimalExponent(String dString)
{
int ePos = dString.indexOf("E");
if (ePos == -1)
{
ePos = dString.indexOf("e");
}
if (ePos != -1)
{
if (dString.length() > ePos + 1)
{
char maybeMinusChar = dString.charAt(ePos + 1);
if (maybeMinusChar != '-')
{
StringBuffer buf = new StringBuffer(dString.length() + 1);
buf.append(dString.substring(0, ePos + 1));
buf.append('+');
buf.append(dString.substring(ePos + 1, dString.length()));
dString = buf.toString();
}
}
}
return dString;
}
/**
* 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(x.length() * 2);
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
{
String TimestampString = null;
synchronized (_TSDF)
{ // SimpleDateFormat is not thread-safe
TimestampString = "'" + _TSDF.format(X) + "'";
}
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 synchronized 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;
_IsNull[parameterIndex - 1] = false;
}
}
/**
* In general, parameter values remain in force for repeated used 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 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;
_IsNull[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.BIT :
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.BIT :
X_as_number =
(Boolean.getBoolean((String) X) ? new Integer("1") : new Integer("0"));
break;
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.BIT:
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 + -