Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring 101: DB Table to return a Map of Code,Description.

 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Here is basic pseudo JDBC code

ResultSet rs= SELECT * FROM CodeDescription (Code, Description)
Loop thru RS
Map.put (rs,getInt(1), rs.getString(2);
return Map;

2. How I can achieve this with Spring?

1M Thanks.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Paul wrote:1. Here is basic pseudo JDBC code

ResultSet rs= SELECT * FROM CodeDescription (Code, Description)
Loop thru RS
Map.put (rs,getInt(1), rs.getString(2);
return Map;

2. How I can achieve this with Spring?

1M Thanks.


We need more information. Spring isn't a JDBC implementation, so what do you want to use? Jdbc and with Spring's JdbcTemplate? Hibernate? Hibernate's implementation of JPA?

With JdbcTemplate there is a queryForMap method, where if you write your sql as a crosstab query, you can make the first column values be the column names and the description the values where you just pass the SQL string to the queryForMap method.

But which data access technology you are using tells us a good deal of what you should do.

Mark
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. (Sorry, if my intention is not clear. Thanks for the pointers)

2. I got what I wanted as shown above code. Just simple JDBC Template is enough.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Paul wrote:

1. (Sorry, if my intention is not clear. Thanks for the pointers)

2. I got what I wanted as shown above code. Just simple JDBC Template is enough.


Yep, that will work perfectly.

The approach that I was talking about would make it the query responsible for setting things up. But writing a crosstab query. But thinking about it, I am not sure you can write a crosstab query for that. But if you did then you can run

Map values = jdbcTemplate.queryForMap(crossTabQueryString);

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic