JDBC doesn't use named parameters as far as I'm aware, but you don't need a library at all for positional parameters, which is enough to "securely replace parameter placeholders for parameters of different data types":
PrepareStatement statement = connection.prepareStatement
("SELECT Foo FROM Bar WHERE Baz = ?");
try {
statement.setString(1, "John O'Reilly"); // No problem with quote here
ResultSet results = statement.executeQuery();
...
} finally {
statement.close();
}
See the JDBC tutorial on prepared statements for more details.
EDIT: If you really want to use something like named parameters, and you're in control of the SQL itself (so you can make sure you use some form that won't be used elsewhere) you could fairly easily create your own query translator:
- Create a
TranslatedQuery
instance (this is your new class) with something like "SELECT Foo FROM Bar WHERE Baz = @Baz"
- Work out the position of each parameter and replace it with
?
- Have one method to return the position for a given name, and another to return the translated SQL
It shouldn't be more than a few hours' work to make the class complete with tests - it doesn't actually need to touch JDBC.