Anand
Anand

Reputation: 25

JdbcTemplate multiple variables in query

I'm trying to run a query like the one below

select col1 from table1 where col2 = ? and col3 = ?

I would like to use JdbcTemplate

can I write like this?

String query = new String("select col1 from table1 where col2 = ? and col3 = ?");
Object[] parameters = new Object[] {new String(col2), new String(col3)};

Object module = jdbcTemplate.queryForObject(query, parameters,"");


**Object module = jdbcTemplate.queryForObject(query, parameters,String.class);** is this right?

Upvotes: 1

Views: 2082

Answers (1)

duffymo
duffymo

Reputation: 308743

JdbcTemplate has several overloaded versions of that method. Which one do you intend to call?

You can add in a RowMapper implementation for the object type you're interested in. That's what I'd recommend.

public class YourRowMapper implements RowMapper<YourClass> {
    YourClass mapRow(ResultSet rs, int rowNum) throws SQLException {
        return new YourClass();  // map the ResultSet row here.
    }
}

Upvotes: 1

Related Questions