public class NamedCallableStatement extends Object
PreparedStatement
and allows the
programmer to set parameters by name instead
of by index.
This eliminates any confusion as to which parameter index
represents what. This also means that
rearranging the SQL statement or adding a parameter doesn't involve
renumbering your indices.
Code such as this:
Connection con=getConnection(); String query="select * from my_table where name=? or address=?"; PreparedStatement p=con.prepareStatement(query); p.setString(1, "bob"); p.setString(2, "123 terrace ct"); ResultSet rs=p.executeQuery();can be replaced with:
Connection con=getConnection(); String query="select * from my_table where name=:name or address=:address"; NamedParameterStatement p=new NamedParameterStatement(con, query); p.setString("name", "bob"); p.setString("address", "123 terrace ct"); ResultSet rs=p.executeQuery();
Constructor and Description |
---|
NamedCallableStatement(Connection connection,
String query)
Creates a NamedParameterStatement.
|
Modifier and Type | Method and Description |
---|---|
void |
addBatch()
Adds the current set of parameters as a batch entry.
|
void |
clearParameters() |
void |
close()
Closes the statement.
|
boolean |
execute()
Executes the statement.
|
int[] |
executeBatch()
Executes all of the batched statements.
|
ResultSet |
executeQuery()
Executes the statement, which must be a query.
|
int |
executeUpdate()
Executes the statement, which must be an SQL INSERT, UPDATE or DELETE
statement;
or an SQL statement that returns nothing, such as a DDL statement.
|
int |
getInt(String name) |
long |
getLong(String name) |
Object |
getObject(String name) |
CallableStatement |
getStatement()
Returns the underlying statement.
|
String |
getString(String name) |
Timestamp |
getTimestamp(String name) |
void |
registerOutParameter(String name,
int sqlType) |
void |
registerOutParameter(String name,
int sqlType,
int scale) |
void |
registerOutParameter(String name,
int sqlType,
String typeName) |
void |
setBigDecimal(String name,
BigDecimal value) |
void |
setBinaryStream(String name,
InputStream input,
int length) |
void |
setBoolean(String name,
boolean value) |
void |
setDate(String name,
Date value) |
void |
setDouble(String name,
double value) |
void |
setInt(String name,
int value)
Sets a parameter.
|
void |
setLong(String name,
long value)
Sets a parameter.
|
void |
setNull(String name,
int sqlType) |
void |
setNull(String name,
int sqlType,
String typeName) |
void |
setObject(String name,
Object value)
Sets a parameter.
|
void |
setObject(String name,
Object value,
int type) |
void |
setString(String name,
String value)
Sets a parameter.
|
void |
setTime(String name,
Time value) |
void |
setTimestamp(String name,
Timestamp value)
Sets a parameter.
|
public NamedCallableStatement(Connection connection, String query) throws SQLException
prepareStatement
.connection
- the database connectionquery
- the parameterized querySQLException
- if the statement could not be createdpublic void clearParameters() throws SQLException
SQLException
public Object getObject(String name) throws SQLException
SQLException
public String getString(String name) throws SQLException
SQLException
public int getInt(String name) throws SQLException
SQLException
public long getLong(String name) throws SQLException
SQLException
public Timestamp getTimestamp(String name) throws SQLException
SQLException
public void registerOutParameter(String name, int sqlType) throws SQLException
SQLException
public void registerOutParameter(String name, int sqlType, int scale) throws SQLException
SQLException
public void registerOutParameter(String name, int sqlType, String typeName) throws SQLException
SQLException
public void setObject(String name, Object value) throws SQLException
name
- parameter namevalue
- parameter valueSQLException
- if an error occurredIllegalArgumentException
- if the parameter does not existPreparedStatement.setObject(int, java.lang.Object)
public void setObject(String name, Object value, int type) throws SQLException
SQLException
public void setString(String name, String value) throws SQLException
name
- parameter namevalue
- parameter valueSQLException
- if an error occurredIllegalArgumentException
- if the parameter does not existPreparedStatement.setString(int, java.lang.String)
public void setInt(String name, int value) throws SQLException
name
- parameter namevalue
- parameter valueSQLException
- if an error occurredIllegalArgumentException
- if the parameter does not existPreparedStatement.setInt(int, int)
public void setLong(String name, long value) throws SQLException
name
- parameter namevalue
- parameter valueSQLException
- if an error occurredIllegalArgumentException
- if the parameter does not existPreparedStatement.setInt(int, int)
public void setTimestamp(String name, Timestamp value) throws SQLException
name
- parameter namevalue
- parameter valueSQLException
- if an error occurredIllegalArgumentException
- if the parameter does not existPreparedStatement.setTimestamp(int, java.sql.Timestamp)
public void setTime(String name, Time value) throws SQLException
SQLException
public void setDate(String name, Date value) throws SQLException
SQLException
public void setBoolean(String name, boolean value) throws SQLException
SQLException
public void setDouble(String name, double value) throws SQLException
SQLException
public void setBigDecimal(String name, BigDecimal value) throws SQLException
SQLException
public void setBinaryStream(String name, InputStream input, int length) throws SQLException
SQLException
public void setNull(String name, int sqlType) throws SQLException
SQLException
public void setNull(String name, int sqlType, String typeName) throws SQLException
SQLException
public CallableStatement getStatement()
public boolean execute() throws SQLException
ResultSet
SQLException
- if an error occurredPreparedStatement.execute()
public ResultSet executeQuery() throws SQLException
SQLException
- if an error occurredPreparedStatement.executeQuery()
public int executeUpdate() throws SQLException
SQLException
- if an error occurredPreparedStatement.executeUpdate()
public void close() throws SQLException
SQLException
- if an error occurredStatement.close()
public void addBatch() throws SQLException
SQLException
- if something went wrongpublic int[] executeBatch() throws SQLException
See Statement.executeBatch()
for details.
SQLException
- if something went wrongCopyright © 2019 OpenEstate. All rights reserved.