java 使用 JdbcTemplate 的 queryForList 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11931340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using queryForList method of JdbcTemplate
提问by ramz
I need to use the following query.
我需要使用以下查询。
SELECT m.member_id, cardflag FROM member m, member_attribute ma WHERE m.member_number=:memberNumber AND m.ref_club_status IN ('A','S') AND m.member_id=ma.member_id
The datatype of member_id
is byte array, and that of cardflag
is varchar.
数据类型member_id
为字节数组,数据类型cardflag
为varchar。
I need to use this query in my code, and looking at the options in jdbctemplate documentation, queryForList seems to be the best choice. I have been trying, but couldnt make out much as to how to pass the parameter to my query and also as to how to handle the return type.
我需要在我的代码中使用这个查询,查看 jdbctemplate 文档中的选项,queryForList 似乎是最好的选择。我一直在尝试,但无法弄清楚如何将参数传递给我的查询以及如何处理返回类型。
Can anyone please help?
有人可以帮忙吗?
Thanks a lot in advance.
非常感谢。
回答by caoxudong
My answer is
我的答案是
String sql = "SELECT m.member_id, searscc FROM member m, member_attribute ma " +
"WHERE m.member_number=:memberNumber AND m.ref_club_status IN ('A','S') AND m.member_id=ma.member_id";
String memberNumber = "John Doe"; //the argument value for the sql above
JdbcTemplate jdbcTemplate = new JdbcTemplate(); //replace this line with your own code to get JdbcTemplate instance.
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, new Object[]{memberNumber}) ;
byte[] tempMemberIds = null;
String tempSearscc = null;
if ((rows != null) || (rows.size() > 0)) {
for (Map<String, Object> tempRow : rows) {
tempMemberIds = (byte[])(tempRow.get("member_id")); //key is your search field in your sql
tempSearscc = (String)(tempRow.get("searscc"));
//do your own jobs
}
} else {
//do something else
}
Hope this useful.
希望这有用。