如何为变量分配列表中的值?
我正在运行此代码:
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上并没有使用
JDBCTemplate
,但是根据文档,queryForList
将返回一个List
地图
a>s,每个Map
中的键是列。因此,要从返回的第一个行中分配这些变量:
如您所见,对于对象类型,您应该能够进行强制转换。对于基元,我已转换为对象等效项,然后使用该对象等效项的方法来获取底层基元(因此对于
int
,转换为Integer
然后使用intValue
< /a>)。I haven't actually used
JDBCTemplate
, but according to the documentation,queryForList
will return aList
ofMap
s, with the keys in eachMap
being 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 toInteger
and then useintValue
).