Search code examples
javaspringoracle10gjdbctemplatespring-3

JDBCTemplate queryForMap for retrieving multiple rows


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?


Solution

  • 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);
    }