• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does Spring Support PreparedStatements Directly?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our application, we use jdbcTemplate and get back the expected: List<Map<...>> as a return type.

When creating a PreparedStatement like this:

preparedStatement = jdbcTemplate.getDataSource().getConnection().prepareStatement("sql statemennt")

it brings back a ResultSet, a totally different data structure. Not what we want.

====


Is there a "spring way" to use a PreparedStatement, but still get back the expected List<Map<...>> data structure?

I'm crafting my own Response from the ResultSet, but it's extremely time consuming.

I'm hoping there's a built-in "Spring way" of using a PreparedStatement and having it return the List<Map<...>> structure.

Appreciate any replies (if possible, with a quick example).

- mike
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no need to create PreparedStatement that way at all. The whole point of using JdbcTemplate is to avoid such boilerplate. Try this:

Javadoc link
If you have a bean class to represent each row, use BeanPropertyRowMapper and get back List<BeanClass> instead of List<Map> .
Note that there are other variants of this method which can also be used to do the same thing. Use whichever is most convenient for your code.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:There's no need to create PreparedStatement that way at all. The whole point of using JdbcTemplate is to avoid such boilerplate. Try this:

Javadoc link
If you have a bean class to represent each row, use BeanPropertyRowMapper and get back List<BeanClass> instead of List<Map> .
Note that there are other variants of this method which can also be used to do the same thing. Use whichever is most convenient for your code.



Thanks very much for your reply.

Looking over the Javadocs from Spring, I saw the other overloaded methods that give me the exact result I needed -- very similar to your post.

I really appreciate the time you took to send me this great info.

--mike
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic