@PublicEvolving public interface FieldNamedPreparedStatement extends AutoCloseable
PreparedStatement
and 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();
Modifier and Type | Method and Description |
---|---|
void |
addBatch()
Adds a set of parameters to this
NamedPreparedStatement object's batch of
commands. |
void |
clearParameters()
Clears the current parameter values immediately.
|
void |
close()
Releases this
Statement object'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.
|
ResultSet |
executeQuery()
Executes the SQL query in this
NamedPreparedStatement object and returns the
ResultSet object generated by the query. |
static FieldNamedPreparedStatement |
prepareStatement(Connection connection,
String sql,
String[] fieldNames)
Creates a
NamedPreparedStatement object for sending parameterized SQL statements
to the database. |
void |
setBigDecimal(int fieldIndex,
BigDecimal x)
Sets the designated parameter to the given
java.math.BigDecimal value. |
void |
setBoolean(int fieldIndex,
boolean x)
Sets the designated parameter to the given Java
boolean value. |
void |
setByte(int fieldIndex,
byte x)
Sets the designated parameter to the given Java
byte value. |
void |
setBytes(int fieldIndex,
byte[] x)
Sets the designated parameter to the given Java array of bytes.
|
void |
setDate(int fieldIndex,
Date x)
Sets the designated parameter to the given
java.sql.Date value using the default
time zone of the virtual machine that is running the application. |
void |
setDouble(int fieldIndex,
double x)
Sets the designated parameter to the given Java
double value. |
void |
setFloat(int fieldIndex,
float x)
Sets the designated parameter to the given Java
float value. |
void |
setInt(int fieldIndex,
int x)
Sets the designated parameter to the given Java
int value. |
void |
setLong(int fieldIndex,
long x)
Sets the designated parameter to the given Java
long value. |
void |
setNull(int fieldIndex,
int sqlType)
Sets the designated parameter to SQL
NULL . |
void |
setObject(int fieldIndex,
Object x)
Sets the value of the designated parameter using the given object.
|
void |
setShort(int fieldIndex,
short x)
Sets the designated parameter to the given Java
short value. |
void |
setString(int fieldIndex,
String x)
Sets the designated parameter to the given Java
String value. |
void |
setTime(int fieldIndex,
Time x)
Sets the designated parameter to the given
java.sql.Time value. |
void |
setTimestamp(int fieldIndex,
Timestamp x)
Sets the designated parameter to the given
java.sql.Timestamp value. |
static FieldNamedPreparedStatement prepareStatement(Connection connection, String sql, String[] fieldNames) throws SQLException
NamedPreparedStatement
object for sending parameterized SQL statements
to the database.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 namesSQLException
void clearParameters() throws SQLException
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
.
SQLException
PreparedStatement.clearParameters()
ResultSet executeQuery() throws SQLException
NamedPreparedStatement
object and returns the
ResultSet
object generated by the query.SQLException
PreparedStatement.executeQuery()
void addBatch() throws SQLException
NamedPreparedStatement
object's batch of
commands.SQLException
PreparedStatement.addBatch()
int[] executeBatch() throws SQLException
int
elements 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.SQLException
Statement.executeBatch()
void setNull(int fieldIndex, int sqlType) throws SQLException
NULL
.
Note: You must specify the parameter's SQL type.
SQLException
PreparedStatement.setNull(int, int)
void setBoolean(int fieldIndex, boolean x) throws SQLException
boolean
value. The driver
converts this to an SQL BIT
or BOOLEAN
value when it sends it to
the database.SQLException
PreparedStatement.setBoolean(int, boolean)
void setByte(int fieldIndex, byte x) throws SQLException
byte
value. The driver converts
this to an SQL TINYINT
value when it sends it to the database.SQLException
PreparedStatement.setByte(int, byte)
void setShort(int fieldIndex, short x) throws SQLException
short
value. The driver converts
this to an SQL SMALLINT
value when it sends it to the database.SQLException
PreparedStatement.setShort(int, short)
void setInt(int fieldIndex, int x) throws SQLException
int
value. The driver converts
this to an SQL INTEGER
value when it sends it to the database.SQLException
PreparedStatement.setInt(int, int)
void setLong(int fieldIndex, long x) throws SQLException
long
value. The driver converts
this to an SQL BIGINT
value when it sends it to the database.SQLException
PreparedStatement.setLong(int, long)
void setFloat(int fieldIndex, float x) throws SQLException
float
value. The driver converts
this to an SQL REAL
value when it sends it to the database.SQLException
PreparedStatement.setFloat(int, float)
void setDouble(int fieldIndex, double x) throws SQLException
double
value. The driver
converts this to an SQL DOUBLE
value when it sends it to the database.SQLException
PreparedStatement.setDouble(int, double)
void setBigDecimal(int fieldIndex, BigDecimal x) throws SQLException
java.math.BigDecimal
value. The
driver converts this to an SQL NUMERIC
value when it sends it to the database.SQLException
PreparedStatement.setBigDecimal(int, BigDecimal)
void setString(int fieldIndex, String x) throws SQLException
String
value. The driver
converts this to an SQL VARCHAR
or LONGVARCHAR
value (depending on
the argument's size relative to the driver's limits on VARCHAR
values) when it
sends it to the database.SQLException
PreparedStatement.setString(int, String)
void setBytes(int fieldIndex, byte[] x) throws SQLException
VARBINARY
or LONGVARBINARY
(depending on the argument's size
relative to the driver's limits on VARBINARY
values) when it sends it to the
database.SQLException
PreparedStatement.setBytes(int, byte[])
void setDate(int fieldIndex, Date x) throws SQLException
java.sql.Date
value using the default
time zone of the virtual machine that is running the application. The driver converts this to
an SQL DATE
value when it sends it to the database.SQLException
PreparedStatement.setDate(int, Date)
void setTime(int fieldIndex, Time x) throws SQLException
java.sql.Time
value. The driver
converts this to an SQL TIME
value when it sends it to the database.SQLException
PreparedStatement.setTime(int, Time)
void setTimestamp(int fieldIndex, Timestamp x) throws SQLException
java.sql.Timestamp
value. The driver
converts this to an SQL TIMESTAMP
value when it sends it to the database.SQLException
PreparedStatement.setTimestamp(int, Timestamp)
void setObject(int fieldIndex, Object x) throws SQLException
SQLException
PreparedStatement.setObject(int, Object)
void close() throws SQLException
Statement
object'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.close
in interface AutoCloseable
SQLException
Statement.close()
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.