How to load data from JDBCTemplate.queryForMap() as it returns the Map Interface? How is the query data maintained internally in the map? I tried to load it, but I got this exception: org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result
Code:-
public List getUserInfoByAlll() {
List profilelist=new ArrayList();
Map m=new HashMap();
m=this.jdbctemplate.queryForMap("SELECT userid,username FROM USER");
Set s=m.keySet();
Iterator it=s.iterator();
while(it.hasNext()){
String its=(String)it.next();
Object ob=(Object)m.get(its);
log.info("UserDAOImpl::getUserListSize()"+ob);
}
return profilelist;
}
queryForMapis appropriate if you want to get a single row. You are selecting without awhereclause, so you probably want toqueryForList. The error is probably indicative of the fact thatqueryForMapwants one row, but you query is retrieving many rows.Check out the docs. There is a
queryForListthat takes just sql; the return type is aList<Map<String,Object>>.So once you have the results, you can do what you are doing. I would do something like
I’ll let you fill in the details, but I would not iterate over keys in this case. I like to explicit about what I am expecting.
If you have a
Userobject, and you actually want to load User instances, you can use thequeryForListthat takes sql and a class typequeryForList(String sql, Class<T> elementType)(wow Spring has changed a lot since I left Javaland.)