How to load data from JDBCTemplate.queryForMap()
and it returns the Map Interface.How the maintained the query data internally in map.I trying to load but i got below exception i.e., 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; }
Plz help me
Here in getDatasource() method we create a new Datasource and configure it. Create a new JdbcTemplate object, with the given datasource to obtain connections from. Use the queryForList(String sql) API method of JdbcTemplate class to execute a query for a result list, with the given static SQL select query.
Usage. Step 1 − Create a JdbcTemplate object using a configured datasource. Step 2 − Use JdbcTemplate object methods to make database operations while parsing the resultset using ResultSetExtractor.
Query for Single Row In Spring, we can use jdbcTemplate. queryForObject() to query a single row record from database, and convert the row into an object via row mapper. 1.2 Spring BeanPropertyRowMapper , this class saves you a lot of time for the mapping.
queryForMap
is appropriate if you want to get a single row. You are selecting without a where
clause, so you probably want to queryForList
. The error is probably indicative of the fact that queryForMap
wants one row, but you query is retrieving many rows.
Check out the docs. There is a queryForList
that takes just sql; the return type is a
List<Map<String,Object>>
.
So once you have the results, you can do what you are doing. I would do something like
List results = template.queryForList(sql); for (Map m : results){ m.get('userid'); m.get('username'); }
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 User
object, and you actually want to load User instances, you can use the queryForList
that takes sql and a class type
queryForList(String sql, Class<T> elementType)
(wow Spring has changed a lot since I left Javaland.)
I know this is really old, but this is the simplest way to query for Map.
Simply implement the ResultSetExtractor interface to define what type you want to return. Below is an example of how to use this. You'll be mapping it manually, but for a simple map, it should be straightforward.
jdbcTemplate.query("select string1,string2 from table where x=1", new ResultSetExtractor<Map>(){ @Override public Map extractData(ResultSet rs) throws SQLException,DataAccessException { HashMap<String,String> mapRet= new HashMap<String,String>(); while(rs.next()){ mapRet.put(rs.getString("string1"),rs.getString("string2")); } return mapRet; } });
This will give you a return type of Map that has multiple rows (however many your query returned) and not a list of Maps. You can view the ResultSetExtractor docs here: http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/jdbc/core/ResultSetExtractor.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With