The spring JDBC template returns me a list which looks like below:
[{CODE_C=CSC, DESC_C=CSC}, {CODE_C=PG1, DESC_C=PG1}]
I want the above list to printed in jsp as below:
CSC-CSC
PG1-PG1
Can you tell me the best way to do this?
The spring JDBC template returns me a list which looks like below:
[{CODE_C=CSC, DESC_C=CSC}, {CODE_C=PG1, DESC_C=PG1}]
I want the above list to printed in jsp as below:
CSC-CSC
PG1-PG1
Can you tell me the best way to do this?
That appears to be a List of Maps.
Assume you've assigned it to a List<Map<?,?>>
named rows
:
for (Map<?,?> row : rows)
out.format("%s-%s ",row.get("CODE_C"),row.get("DESC_C"));
List<String> results = new ArrayList<String>();
for (Map<String, String> map : someList) {
StringBuilder sb = new StringBuilder();
for (String s : map.values()) {
sb.append(s).append('-');
}
String result = sb.toString();
results.add(result.substring(0, result.length() - 1); // To cut the trailing '-'
}
// Do whatever with the results.
<c:forEach var="map" items="${list_with_maps_inside}">
${map['CODE_C']} ${map['DESC_C']}
</c:forEach>
Although not really your question, I think it is best to use a Mapper to retrieve the results. If you do so, you will get a list of objects, which you can easily iterate using a for-loop.
Example from http://static.springsource.org/spring/docs/2.0.x/reference/jdbc.html:
public Collection findAllActors() {
return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());
}
private static final class ActorMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setSurname(rs.getString("surname"));
return actor;
}
}