Class NamedParamStatement

java.lang.Object
fc.jdbc.NamedParamStatement

public final class NamedParamStatement extends Object
This class wraps around 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:

select * from foo where search = @search-term and radius = '@radius::int'
This contains two named parameters, @search-term and @radius. To use this in code, we say:

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).