public final class NamedParamStatement extends java.lang.Object
PreparedStatement
and allows the programmer to set
parameters by name instead of by question mark index.
Inspired by a similar concept at: javaworld (although this class was rewritten from scratch).
Named parameters are written as @foo (that is, they start with a @ character). Named parameters can only contain alphanumeric, underscore and dashes, any character not in this allowed list automatically ends the named parameter and continues normal SQL. For example:
This contains two named parameters, @search-term and @radius. To use this in code, we say:select * from foo where search = @search-term and radius = '@radius::int'
Connection con = getConnection(); //some way to get a connection String query = "select * from foo where search = @search-term and radius = '@radius::int'"; NamedParamStatement ps = NamedParamStatement.get(con, query); ps.setString("search-term", "hello world"); ps.setInt("radius", 42); ResultSet rs = ps.executeQuery();
Note: When setting a named paramter, the "@" must be omitted.
The same named parameter can appear multiple times in the query, and is replaced wherever it appears by its value.
The close()
method should be called to release resources and help with
garbage collection (right around the time close is called on the
associated connection, which is after any retrieved data/resultset has
been fully read).
Modifier and Type | Method and Description |
---|---|
void |
addBatch() |
void |
clearParameters() |
void |
close() |
boolean |
execute() |
java.sql.ResultSet |
executeQuery() |
int |
executeUpdate() |
static NamedParamStatement |
get(java.sql.Connection con,
java.lang.String query)
Returns a new instance of NamedParamStatement.
|
java.sql.ResultSetMetaData |
getMetaData() |
java.sql.ParameterMetaData |
getParameterMetaData() |
static NamedParamStatement |
getScrollable(java.sql.Connection con,
java.lang.String query)
Returns a new instance of NamedParamStatement.
|
void |
setArray(java.lang.String name,
java.sql.Array x) |
void |
setAsciiStream(java.lang.String name,
java.io.InputStream x,
int length) |
void |
setBigDecimal(java.lang.String name,
java.math.BigDecimal x) |
void |
setBinaryStream(java.lang.String name,
java.io.InputStream x,
int length) |
void |
setBlob(java.lang.String name,
java.sql.Blob x) |
void |
setBoolean(java.lang.String name,
boolean x) |
void |
setByte(java.lang.String name,
byte x) |
void |
setBytes(java.lang.String name,
byte[] x) |
void |
setCharacterStream(java.lang.String name,
java.io.Reader reader,
int length) |
void |
setClob(java.lang.String name,
java.sql.Clob x) |
void |
setDate(java.lang.String name,
java.sql.Date x) |
void |
setDate(java.lang.String name,
java.sql.Date x,
java.util.Calendar cal) |
void |
setDouble(java.lang.String name,
double x) |
void |
setFloat(java.lang.String name,
float x) |
void |
setInt(java.lang.String name,
int x) |
void |
setLong(java.lang.String name,
long x) |
void |
setNull(java.lang.String name,
int sqlType) |
void |
setNull(java.lang.String name,
int sqlType,
java.lang.String typeName) |
void |
setObject(java.lang.String name,
java.lang.Object x) |
void |
setObject(java.lang.String name,
java.lang.Object x,
int targetSqlType) |
void |
setObject(java.lang.String name,
java.lang.Object x,
int targetSqlType,
int scale) |
void |
setRef(java.lang.String name,
java.sql.Ref x) |
void |
setShort(java.lang.String name,
short x) |
void |
setString(java.lang.String name,
java.lang.String x) |
void |
setTime(java.lang.String name,
java.sql.Time x) |
void |
setTime(java.lang.String name,
java.sql.Time x,
java.util.Calendar cal) |
void |
setTimestamp(java.lang.String name,
java.sql.Timestamp x) |
void |
setTimestamp(java.lang.String name,
java.sql.Timestamp x,
java.util.Calendar cal) |
void |
setURL(java.lang.String name,
java.net.URL x) |
java.lang.String |
toString() |
public static NamedParamStatement get(java.sql.Connection con, java.lang.String query) throws java.sql.SQLException
getScrollable(java.sql.Connection, java.lang.String)
for
a scrollable result set).java.sql.SQLException
public static NamedParamStatement getScrollable(java.sql.Connection con, java.lang.String query) throws java.sql.SQLException
PreparedStatement
. (the query string is not reparsed
every time, just the first time this method is invoked for any particular
query).
This method ensures that any ResultSet
returned by the wrapped
PreparedStatement is scrollable (the ResultSet.TYPE_SCROLL_INSENSITIVE
flag is used when creating the PreparedStatement).
java.sql.SQLException
public void close() throws java.sql.SQLException
java.sql.SQLException
public java.lang.String toString()
toString
in class java.lang.Object
public java.sql.ResultSet executeQuery() throws java.sql.SQLException
java.sql.SQLException
public int executeUpdate() throws java.sql.SQLException
java.sql.SQLException
public void setNull(java.lang.String name, int sqlType) throws java.sql.SQLException
java.sql.SQLException
public void setBoolean(java.lang.String name, boolean x) throws java.sql.SQLException
java.sql.SQLException
public void setByte(java.lang.String name, byte x) throws java.sql.SQLException
java.sql.SQLException
public void setShort(java.lang.String name, short x) throws java.sql.SQLException
java.sql.SQLException
public void setInt(java.lang.String name, int x) throws java.sql.SQLException
java.sql.SQLException
public void setLong(java.lang.String name, long x) throws java.sql.SQLException
java.sql.SQLException
public void setFloat(java.lang.String name, float x) throws java.sql.SQLException
java.sql.SQLException
public void setDouble(java.lang.String name, double x) throws java.sql.SQLException
java.sql.SQLException
public void setBigDecimal(java.lang.String name, java.math.BigDecimal x) throws java.sql.SQLException
java.sql.SQLException
public void setString(java.lang.String name, java.lang.String x) throws java.sql.SQLException
java.sql.SQLException
public void setBytes(java.lang.String name, byte[] x) throws java.sql.SQLException
java.sql.SQLException
public void setDate(java.lang.String name, java.sql.Date x) throws java.sql.SQLException
java.sql.SQLException
public void setTime(java.lang.String name, java.sql.Time x) throws java.sql.SQLException
java.sql.SQLException
public void setTimestamp(java.lang.String name, java.sql.Timestamp x) throws java.sql.SQLException
java.sql.SQLException
public void setAsciiStream(java.lang.String name, java.io.InputStream x, int length) throws java.sql.SQLException
java.sql.SQLException
public void setBinaryStream(java.lang.String name, java.io.InputStream x, int length) throws java.sql.SQLException
java.sql.SQLException
public void clearParameters() throws java.sql.SQLException
java.sql.SQLException
public void setObject(java.lang.String name, java.lang.Object x, int targetSqlType, int scale) throws java.sql.SQLException
java.sql.SQLException
public void setObject(java.lang.String name, java.lang.Object x, int targetSqlType) throws java.sql.SQLException
java.sql.SQLException
public void setObject(java.lang.String name, java.lang.Object x) throws java.sql.SQLException
java.sql.SQLException
public boolean execute() throws java.sql.SQLException
java.sql.SQLException
public void addBatch() throws java.sql.SQLException
java.sql.SQLException
public void setCharacterStream(java.lang.String name, java.io.Reader reader, int length) throws java.sql.SQLException
java.sql.SQLException
public void setRef(java.lang.String name, java.sql.Ref x) throws java.sql.SQLException
java.sql.SQLException
public void setBlob(java.lang.String name, java.sql.Blob x) throws java.sql.SQLException
java.sql.SQLException
public void setClob(java.lang.String name, java.sql.Clob x) throws java.sql.SQLException
java.sql.SQLException
public void setArray(java.lang.String name, java.sql.Array x) throws java.sql.SQLException
java.sql.SQLException
public java.sql.ResultSetMetaData getMetaData() throws java.sql.SQLException
java.sql.SQLException
public void setDate(java.lang.String name, java.sql.Date x, java.util.Calendar cal) throws java.sql.SQLException
java.sql.SQLException
public void setTime(java.lang.String name, java.sql.Time x, java.util.Calendar cal) throws java.sql.SQLException
java.sql.SQLException
public void setTimestamp(java.lang.String name, java.sql.Timestamp x, java.util.Calendar cal) throws java.sql.SQLException
java.sql.SQLException
public void setNull(java.lang.String name, int sqlType, java.lang.String typeName) throws java.sql.SQLException
java.sql.SQLException
public void setURL(java.lang.String name, java.net.URL x) throws java.sql.SQLException
java.sql.SQLException
public java.sql.ParameterMetaData getParameterMetaData() throws java.sql.SQLException
java.sql.SQLException