File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Using bind variables in Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Using bind variables in Spring" Watch "Using bind variables in Spring" New topic

Using bind variables in Spring

J Miller
Ranch Hand

Joined: Oct 21, 2010
Posts: 68
I'm doing some testing that involves database access using spring framework. I understand that I can create a query using a single bind variable using the following statement:

int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt(
"select count(*) from t_actor where first_name = ?", "Joe");

I'm trying to use the queryForObject implementation described in the Spring docs (url at bottom). I have two questions about this. First I'm trying a simple implementation like this:

But it's giving me an error under queryForObject saying
The method queryForObject(String, Object[], RowMapper) in the type JdbcTemplate is not applicable for the arguments (String, String, ActorMapper)

So my first question is how do I get that working?

My second question is how do I use multiple bind variables? Let's say I wanted to say

SELECT * FROM actors WHERE first_name = XXX AND last_name = YYY"

How would I do that?

Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

The signature is queryForObject(String, Object[], RowMapper)

So you have to pass

Multiple variables

===Vyas Sanzgiri===
My Blog
J Miller
Ranch Hand

Joined: Oct 21, 2010
Posts: 68
You were absolutely right, thanks for the help. Another tip that I found is this: I then tried using LIKE rather than = using this:

This also threw me an error. Apparently you can't use '?%', you have to change it to something like this:

Thanks for the help!
I agree. Here's the link:
subject: Using bind variables in Spring