Interface FieldNamedPreparedStatement
-
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
FieldNamedPreparedStatementImpl
@PublicEvolving public interface FieldNamedPreparedStatement extends AutoCloseable
This is a wrapper aroundPreparedStatementand allows the users to set parameters by name instead of by index. This allows users to use the same variable parameter multiple times in a statement.Code such as this:
Connection con = getConnection(); String query = "select * from my_table where first_name=? or last_name=?"; PreparedStatement st = con.prepareStatement(query); st.setString(1, "bob"); st.setString(2, "bob"); ResultSet rs = st.executeQuery();
Can be replaced with:
Connection con = getConnection(); String query = "select * from my_table where first_name=:name or last_name=:name"; FieldNamedPreparedStatement st = FieldNamedPreparedStatement.prepareStatement(con, query, new String[]{"name"}); st.setString(0, "bob"); ResultSet rs = st.executeQuery();
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddBatch()Adds a set of parameters to thisNamedPreparedStatementobject's batch of commands.voidclearParameters()Clears the current parameter values immediately.voidclose()Releases thisStatementobject's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.int[]executeBatch()Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.ResultSetexecuteQuery()Executes the SQL query in thisNamedPreparedStatementobject and returns theResultSetobject generated by the query.static FieldNamedPreparedStatementprepareStatement(Connection connection, String sql, String[] fieldNames)Creates aNamedPreparedStatementobject for sending parameterized SQL statements to the database.voidsetBigDecimal(int fieldIndex, BigDecimal x)Sets the designated parameter to the givenjava.math.BigDecimalvalue.voidsetBoolean(int fieldIndex, boolean x)Sets the designated parameter to the given Javabooleanvalue.voidsetByte(int fieldIndex, byte x)Sets the designated parameter to the given Javabytevalue.voidsetBytes(int fieldIndex, byte[] x)Sets the designated parameter to the given Java array of bytes.voidsetDate(int fieldIndex, Date x)Sets the designated parameter to the givenjava.sql.Datevalue using the default time zone of the virtual machine that is running the application.voidsetDouble(int fieldIndex, double x)Sets the designated parameter to the given Javadoublevalue.voidsetFloat(int fieldIndex, float x)Sets the designated parameter to the given Javafloatvalue.voidsetInt(int fieldIndex, int x)Sets the designated parameter to the given Javaintvalue.voidsetLong(int fieldIndex, long x)Sets the designated parameter to the given Javalongvalue.voidsetNull(int fieldIndex, int sqlType)Sets the designated parameter to SQLNULL.voidsetObject(int fieldIndex, Object x)Sets the value of the designated parameter using the given object.voidsetShort(int fieldIndex, short x)Sets the designated parameter to the given Javashortvalue.voidsetString(int fieldIndex, String x)Sets the designated parameter to the given JavaStringvalue.voidsetTime(int fieldIndex, Time x)Sets the designated parameter to the givenjava.sql.Timevalue.voidsetTimestamp(int fieldIndex, Timestamp x)Sets the designated parameter to the givenjava.sql.Timestampvalue.
-
-
-
Method Detail
-
prepareStatement
static FieldNamedPreparedStatement prepareStatement(Connection connection, String sql, String[] fieldNames) throws SQLException
Creates aNamedPreparedStatementobject for sending parameterized SQL statements to the database.- Parameters:
connection- the connection used to connect to database.sql- an SQL statement that may contain one or more ':fieldName' as parameter placeholdersfieldNames- the field names in schema order used as the parameter names- Throws:
SQLException
-
clearParameters
void clearParameters() throws SQLExceptionClears 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:
SQLException- See Also:
PreparedStatement.clearParameters()
-
executeQuery
ResultSet executeQuery() throws SQLException
Executes the SQL query in thisNamedPreparedStatementobject and returns theResultSetobject generated by the query.- Throws:
SQLException- See Also:
PreparedStatement.executeQuery()
-
addBatch
void addBatch() throws SQLExceptionAdds a set of parameters to thisNamedPreparedStatementobject's batch of commands.- Throws:
SQLException- See Also:
PreparedStatement.addBatch()
-
executeBatch
int[] executeBatch() throws SQLExceptionSubmits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. Theintelements of the array that is returned are ordered to correspond to the commands in the batch, which are ordered according to the order in which they were added to the batch.- Throws:
SQLException- See Also:
Statement.executeBatch()
-
setNull
void setNull(int fieldIndex, int sqlType) throws SQLExceptionSets the designated parameter to SQLNULL.Note: You must specify the parameter's SQL type.
- Throws:
SQLException- See Also:
PreparedStatement.setNull(int, int)
-
setBoolean
void setBoolean(int fieldIndex, boolean x) throws SQLExceptionSets the designated parameter to the given Javabooleanvalue. The driver converts this to an SQLBITorBOOLEANvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setBoolean(int, boolean)
-
setByte
void setByte(int fieldIndex, byte x) throws SQLExceptionSets the designated parameter to the given Javabytevalue. The driver converts this to an SQLTINYINTvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setByte(int, byte)
-
setShort
void setShort(int fieldIndex, short x) throws SQLExceptionSets the designated parameter to the given Javashortvalue. The driver converts this to an SQLSMALLINTvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setShort(int, short)
-
setInt
void setInt(int fieldIndex, int x) throws SQLExceptionSets the designated parameter to the given Javaintvalue. The driver converts this to an SQLINTEGERvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setInt(int, int)
-
setLong
void setLong(int fieldIndex, long x) throws SQLExceptionSets the designated parameter to the given Javalongvalue. The driver converts this to an SQLBIGINTvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setLong(int, long)
-
setFloat
void setFloat(int fieldIndex, float x) throws SQLExceptionSets the designated parameter to the given Javafloatvalue. The driver converts this to an SQLREALvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setFloat(int, float)
-
setDouble
void setDouble(int fieldIndex, double x) throws SQLExceptionSets the designated parameter to the given Javadoublevalue. The driver converts this to an SQLDOUBLEvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setDouble(int, double)
-
setBigDecimal
void setBigDecimal(int fieldIndex, BigDecimal x) throws SQLExceptionSets the designated parameter to the givenjava.math.BigDecimalvalue. The driver converts this to an SQLNUMERICvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setBigDecimal(int, BigDecimal)
-
setString
void setString(int fieldIndex, String x) throws SQLExceptionSets the designated parameter to the given JavaStringvalue. The driver converts this to an SQLVARCHARorLONGVARCHARvalue (depending on the argument's size relative to the driver's limits onVARCHARvalues) when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setString(int, String)
-
setBytes
void setBytes(int fieldIndex, byte[] x) throws SQLExceptionSets the designated parameter to the given Java array of bytes. The driver converts this to an SQLVARBINARYorLONGVARBINARY(depending on the argument's size relative to the driver's limits onVARBINARYvalues) when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setBytes(int, byte[])
-
setDate
void setDate(int fieldIndex, Date x) throws SQLExceptionSets the designated parameter to the givenjava.sql.Datevalue using the default time zone of the virtual machine that is running the application. The driver converts this to an SQLDATEvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setDate(int, Date)
-
setTime
void setTime(int fieldIndex, Time x) throws SQLExceptionSets the designated parameter to the givenjava.sql.Timevalue. The driver converts this to an SQLTIMEvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setTime(int, Time)
-
setTimestamp
void setTimestamp(int fieldIndex, Timestamp x) throws SQLExceptionSets the designated parameter to the givenjava.sql.Timestampvalue. The driver converts this to an SQLTIMESTAMPvalue when it sends it to the database.- Throws:
SQLException- See Also:
PreparedStatement.setTimestamp(int, Timestamp)
-
setObject
void setObject(int fieldIndex, Object x) throws SQLExceptionSets the value of the designated parameter using the given object.- Throws:
SQLException- See Also:
PreparedStatement.setObject(int, Object)
-
close
void close() throws SQLExceptionReleases thisStatementobject's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException- See Also:
Statement.close()
-
-