File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes better option JdbcTemplate.queryForList(query) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "better option JdbcTemplate.queryForList(query)" Watch "better option JdbcTemplate.queryForList(query)" New topic

better option JdbcTemplate.queryForList(query)

amol bakre

Joined: May 21, 2001
Posts: 29
Hi There,

I am new to springs and needed help / opinion.

The thing that i am trying to do is to sue jdbcTemplate's queryForList method to return some data from the DB. This data i pass on to the webpage that expects a lists and takes care of the display etc.

The problem is that the queryForList method is returning to me data in the following form

whereas what i need is just the data. Ofourse i can extract data and stuff, but that i think is dirty and surely there must be some other way to get a List in return (no dao's etc).

Also note i have only one column in the result of my query, stumbled upon SingleColumnRowMapper etc, but did not figure out much how to use them to get a List in return.

Please suggest, preferrably with code example..
Would appreciate a quick response..

Thanks a lot in advance..
Nickolas Case
Ranch Hand

Joined: Apr 26, 2008
Posts: 98
If I understand your issue, I think you might want to consider using simpleJdbcDaoSupport. The method Query(String sql, ParameterizedRowMapper<T>, Object... args) should do the trick for you. Use the ParameterizedRowMapper to add the results to your list.

Something like this

If you have to use JdbcTemplate, I would use the method query(String sql, RowMapper mapper). Then you could use the singleColumnRowMapper to map the rows to your list.

I imagine a ParameterizedSingleColumnRowMapper would do the same.
Hope this helps!
amol bakre

Joined: May 21, 2001
Posts: 29

O many thanks..i was using JdbcTemplate and i used the query method passing it the SingleColumnRowMapper as you had mentioned in the last, worked well..and was quite easy too, have to nothing but just pass a new instance..

Actually i was using it to get a list so that i can throw that back to the struts2 jsp page (via action etc..) to be displayed in a dropdown list and that is the reason i was looking for just a list..and getting a list and letting struts do all the work seemed a simple way for me to do this..

Thanks a ton..

I agree. Here's the link:
subject: better option JdbcTemplate.queryForList(query)
It's not a secret anymore!