Lenusska1414
Lenusska1414

Reputation: 171

How to use select WHERE clause in jdbcTemplateObject?

I need to use query like this:

"select * from Team_has_users where Teams_id_team = ?";

If I call this method:

    public Team_has_users listTeamMembers(int id_team) {
    String SQL = "select * from Team_has_users where Teams_id_team = ?";
    Team_has_users members = jdbcTemplateObject.queryForObject(SQL, new Object[] { id_team },
            new Team_has_usersMapper());
    return members;
}

I get exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 2

I have more then one object in DB with the same id_team, so I understand, why it is throwing an excepction, but I need some solution to get all the record from DB.

I thought about using it in method like this, but I don't know how to use it with parameter "id_team".

    public List<Team_has_users> listTeamMembers(int id_team) {
    String SQL = "select * from Team_has_users where Teams_id_team = ?";
    List<Team_has_users> members = jdbcTemplateObject.query(SQL, new Team_has_usersMapper());
    return members;
}

Any ideas?

Upvotes: 0

Views: 4993

Answers (1)

victor gallet
victor gallet

Reputation: 1898

You are right, you have to use query. Just pass the argument to the function :

List<Team_has_users> members =  getJdbcTemplate().query(SQL, new Team_has_usersMapper(), id_team);

By the way, you can check the jdbcTemplate javadoc.

Upvotes: 2

Related Questions