📄 jdbcpreparedstatement.java
字号:
Timestamp x) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setParameter(parameterIndex, "'" + x.toString() + "'");
}
}
/**
* 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. 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, the second is 2, ...
* @param x the Java input stream that contains the ASCII parameter value
* @param length the number of bytes in the stream
* @exception SQLException if a database access error occurs
*/
public void setAsciiStream(int parameterIndex, InputStream x,
int length) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
} else {
setString(parameterIndex, StringConverter.InputStreamToString(x));
}
}
/**
* 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. 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.
* The byte format of the Unicode stream must be Java UTF-8, as
* defined in the Java Virtual Machine Specification.
*
* <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, the second is 2, ...
* @param x the java input stream which contains the
* UNICODE parameter value
* @param length the number of bytes in the stream
* @exception SQLException if a database access error occurs
* @deprecated
*/
public void setUnicodeStream(int paramIndex, InputStream x,
int length) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setAsciiStream(paramIndex, x, length);
}
/**
* 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. 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, the second is 2, ...
* @param x the java input stream which contains the binary parameter value
* @param length the number of bytes in the stream
* @exception SQLException if a database access error occurs
*/
public void setBinaryStream(int parameterIndex, InputStream x,
int length) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
byte b[] =
new byte[length]; // todo: is this correct? what if length=0?
try {
x.read(b, 0, length);
x.close();
} catch (IOException e) {
throw Trace.error(Trace.INPUTSTREAM_ERROR, e.getMessage());
}
setBytes(parameterIndex, b);
}
/**
* Clears the current parameter values immediately.
* <P>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 clearParameters.
*/
public void clearParameters() {
if (Trace.TRACE) {
Trace.trace();
}
vParameter.removeAllElements();
}
/**
* <p>Sets the value of a parameter using an object. The second
* argument must be an object type; for integral values, the
* java.lang equivalent objects should be used.
*
* <p>The given Java object will be converted to the targetSqlType
* before being sent to the database.
*
* If the object has a custom mapping (is of a class implementing SQLData),
* the JDBC driver should call its method <code>writeSQL</code> to write it
* to the SQL data stream.
* If, on the other hand, the object is of a class implementing
* Ref, Blob, Clob, Struct,
* or Array, the driver should pass it to the database as a value of the
* corresponding SQL type.
*
* <p>Note that this method may be used to pass datatabase-
* 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 scale for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types,
* this is the number of digits after the decimal point. For all other
* types, this value will be ignored.
* @exception SQLException if a database access error occurs
*/
public void setObject(int i, Object x, int type,
int scale) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
setObject(i, x, type);
}
/**
* Sets the value of the designated parameter with the given object.
* This method is like 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
* @exception SQLException if a database access error occurs
*/
public void setObject(int parameterIndex, Object x,
int type) throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
return;
}
x = Column.convertObject(x, type);
setObjectInType(parameterIndex, x, type);
}
/**
* <p>Sets 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.
*
* If the object is of a class implementing SQLData,
* the JDBC driver should call its method <code>writeSQL</code> to write it
* to the SQL data stream.
* If, on the other hand, the object is of a class implementing
* Ref, Blob, Clob, Struct,
* or Array, then the driver should pass it to the database as a value of the
* corresponding SQL type.
*
* This method throws an exception if there is an ambiguity, for example, if
* the object is of a class implementing more than one of those interfaces.
*
* <P><font color="#009900">
* In HSQL Database Engine, this function checks if the object is of one of the
* Java SQL Types (Integer, String, Double,...) and if yes, the function will
* call the apropriate setXXX function. If an object should be inserted
* with data type 'Object' (OTHER), then the function
* setObject(i,x,Types.OTHER) should be called.
* </font><P>
*
* @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 (Trace.TRACE) {
Trace.trace();
}
if (x == null) {
setNull(parameterIndex);
return;
}
int type = Types.OTHER;
if (x instanceof String) {
type = Types.VARCHAR;
} else if (x instanceof BigDecimal) {
type = Types.NUMERIC;
} else if (x instanceof Integer) {
type = Types.INTEGER;
} else if (x instanceof Long) {
type = Types.BIGINT;
} else if (x instanceof Float) {
type = Types.REAL;
} else if (x instanceof Double) {
type = Types.DOUBLE;
} else if (x instanceof byte[]) {
type = Types.BINARY;
} else if (x instanceof java.sql.Date) {
type = Types.DATE;
} else if (x instanceof Time) {
type = Types.TIME;
} else if (x instanceof Timestamp) {
type = Types.TIMESTAMP;
} else if (x instanceof Boolean) {
type = Types.BIT;
} else if (x instanceof Byte) {
type = Types.TINYINT;
} else if (x instanceof Short) {
type = Types.SMALLINT;
}
setObjectInType(parameterIndex, x, type);
}
/**
* Executes any kind of SQL statement.
* Some prepared statements return multiple results; the execute
* method handles these complex statements as well as the simpler
* form of statements handled by executeQuery and executeUpdate.
*
* @exception SQLException if a database access error occurs
* @see Statement#execute
*/
public boolean execute() throws SQLException {
if (Trace.TRACE) {
Trace.trace();
}
return super.execute(build());
}
/**
* Method declaration
*
*
* @param i
* @param s
*/
private void setParameter(int i, String s) {
if (Trace.TRACE) {
Trace.trace();
}
if (vParameter.size() < i) {
vParameter.setSize(i);
}
vParameter.setElementAt(s, --i);
}
/**
* Method declaration
*
*
* @param i
*
* @return
*/
private String getParameter(int i) {
if (i >= vParameter.size()) {
return null;
}
return (String) vParameter.elementAt(i);
}
/**
* Method declaration
*
*
* @return
*/
private String build() {
if (vParameter.isEmpty()) {
return sSql;
}
StringBuffer s = new StringBuffer();
int i = 0, l = sSql.length();
boolean bSkip = false, bSkip2 = false;
for (int j = 0; j < l; j++) {
char c = sSql.charAt(j);
if (c == '?' &&!bSkip &&!bSkip2) {
s.append(getParameter(i++));
} else {
if (c == '\'' &&!bSkip2) {
bSkip = !bSkip;
} else if (c == '"' &&!bSkip) {
bSkip2 = !bSkip2;
}
s.append(c);
}
}
return s.toString();
}
/**
* Method declaration
*
*
* @param parameterIndex
*/
private void setNull(int parameterIndex) {
setParameter(parameterIndex, "NULL");
}
// implementation of CallableStatement
/**
* Registers the OUT parameter in ordinal position
* <code>parameterIndex</code> to the JDBC type
* <code>sqlType</code>. All OUT parameters must be registered
* before a stored procedure is executed.
* <p>
* The JDBC type specified by <code>sqlType</code> for an OUT
* parameter determines the Java type that must be used
* in the <code>get</code> method to read the value of that parameter.
* <p>
* If the JDBC type expected to be returned to this output parameter
* is specific to this particular database, <code>sqlType</code>
* should be <code>java.sql.Types.OTHER</code>. The method
* {@link #getObject} retrieves the value.
* @param parameterIndex the first parameter is 1, the second is 2,
* and so on
* @param sqlType the JDBC type code defined by <code>java.sql.Types</code>.
* If the parameter is of type Numeric or Decimal, the version of
* <code>registerOutParameter</code> that accepts a scale value
* should be used.
* @exception SQLException if a database access error occurs
*/
public void registerOutParameter(int parameterIndex,
int sqlType) throws SQLException {
throw getNotSupported();
}
/**
* Registers the parameter in ordinal position
* <code>parameterIndex</code> to be of JDBC type
* <code>sqlType</code>. This method must be called
* before a stored procedure is executed.
* <p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -