Class NamedParamStatement
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).
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddBatch()voidvoidclose()booleanexecute()intstatic NamedParamStatementget(Connection con, String query) Returns a new instance of NamedParamStatement.static NamedParamStatementgetScrollable(Connection con, String query) Returns a new instance of NamedParamStatement.voidvoidsetAsciiStream(String name, InputStream x, int length) voidsetBigDecimal(String name, BigDecimal x) voidsetBinaryStream(String name, InputStream x, int length) voidvoidsetBoolean(String name, boolean x) voidvoidvoidsetCharacterStream(String name, Reader reader, int length) voidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidsetTimestamp(String name, Timestamp x) voidsetTimestamp(String name, Timestamp x, Calendar cal) voidtoString()
-
Method Details
-
get
Returns a new instance of NamedParamStatement. This instance, internally, creates/wraps a new prepared statement. (the query string is not reparsed every time, just the first time this method is invoked for any particular query). The returned query is not scrollable (usegetScrollable(Connection, String)for a scrollable result set).- Throws:
SQLException
-
getScrollable
Returns a new instance of NamedParamStatement. This instance, internally, creates/wraps a newPreparedStatement. (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
ResultSetreturned by the wrapped PreparedStatement is scrollable (the ResultSet.TYPE_SCROLL_INSENSITIVE flag is used when creating the PreparedStatement).- Throws:
SQLException
-
close
- Throws:
SQLException
-
toString
-
executeQuery
- Throws:
SQLException
-
executeUpdate
- Throws:
SQLException
-
setNull
- Throws:
SQLException
-
setBoolean
- Throws:
SQLException
-
setByte
- Throws:
SQLException
-
setShort
- Throws:
SQLException
-
setInt
- Throws:
SQLException
-
setLong
- Throws:
SQLException
-
setFloat
- Throws:
SQLException
-
setDouble
- Throws:
SQLException
-
setBigDecimal
- Throws:
SQLException
-
setString
- Throws:
SQLException
-
setBytes
- Throws:
SQLException
-
setDate
- Throws:
SQLException
-
setTime
- Throws:
SQLException
-
setTimestamp
- Throws:
SQLException
-
setAsciiStream
- Throws:
SQLException
-
setBinaryStream
- Throws:
SQLException
-
clearParameters
- Throws:
SQLException
-
setObject
- Throws:
SQLException
-
setObject
- Throws:
SQLException
-
setObject
- Throws:
SQLException
-
execute
- Throws:
SQLException
-
addBatch
- Throws:
SQLException
-
setCharacterStream
- Throws:
SQLException
-
setRef
- Throws:
SQLException
-
setBlob
- Throws:
SQLException
-
setClob
- Throws:
SQLException
-
setArray
- Throws:
SQLException
-
getMetaData
- Throws:
SQLException
-
setDate
- Throws:
SQLException
-
setTime
- Throws:
SQLException
-
setTimestamp
- Throws:
SQLException
-
setNull
- Throws:
SQLException
-
setURL
- Throws:
SQLException
-
getParameterMetaData
- Throws:
SQLException
-