JDBCTemplate queryForMap for retrieving multiple rows

user182944 picture user182944 · Jun 14, 2014 · Viewed 47.6k times · Source

Can I use queryForMap if there are multiple rows returned by the query.

For a single row, the below code works fine.

public Map<String, Object> retrieveMultipleRowsColumns(String deptName){
    return jdbcTemplate.queryForMap("SELECT DEPT_ID,DEPT_NAME FROM DEPT WHERE DEPT_NAME = ?", deptName);
}

How to modify this for multiple rows?

Answer

John Farrelly picture John Farrelly · Jun 14, 2014

Use queryForList see the javadoc for full details. It returns List<Map<String,Object>>

public List<Map<String, Object>> retrieveMultipleRowsColumns(String deptName){
    return jdbcTemplate.queryForList("SELECT DEPT_ID,DEPT_NAME FROM DEPT WHERE DEPT_NAME = ?", deptName);
}