I am running this code:
int key = 25;
String query = "Select one, two, three, four, five from myTable where key=?";
List<Map<String,Object>> data = jdbcTemplate.queryForList(query, new Object[]{key});
//one is string, two is int, three is character, four is double, five is string
String one = null;
int two = 0;
char three = '\u0000';
double four = 0.0;
String five = null;
I want to set the five variables above with the values returned in the list. How?
I haven’t actually used
JDBCTemplate, but according to the documentation,queryForListwill return aListofMaps, with the keys in eachMapbeing the names of the columns.So to assign those variables from the first returned row:
As you can see, for the object types, you should just be able to cast. For the primitives, I’ve cast to the object equivalent and then used that object equivalent’s method for getting the underlying primitive (so for
int, cast toIntegerand then useintValue).