Are there named parameters in JDBC instead of positional ones, like the @name
, @city
in the ADO.NET query below?
select * from customers where name=@name and city = @city
Are there named parameters in JDBC instead of positional ones, like the @name
, @city
in the ADO.NET query below?
select * from customers where name=@name and city = @city
JDBC does not support named parameters. Unless you are bound to using plain JDBC (which causes pain, let me tell you that) I would suggest to use Springs Excellent JDBCTemplate which can be used without the whole IoC Container.
NamedParameterJDBCTemplate supports named parameters, you can use them like that:
To avoid including a large framework, I think a simple homemade class can do the trick.
Example of class to handle named parameters:
Example of calling the class:
Please note that the above simple example does not handle using named parameter twice. Nor does it handle using the : sign inside quotes.
Vanilla JDBC only supports named parameters in a
CallableStatement
(e.g.setString("name", name)
), and even then, I suspect the underlying stored procedure implementation has to support it.An example of how to use named parameters:
You can't use named parameters in JDBC itself. You could try using Spring framework, as it has some extensions that allow the use of named parameters in queries.
Plain vanilla JDBC does not support named parameters.
If you are using DB2 then using DB2 classes directly: