这是我使用 jdbcTemplate 的代码
String SQL = "select branch from branchTable where branch_name = '" + branch + "'";
ArrayList<String> branchList = (ArrayList<String>) jdbcTemplateObject.query(SQL, new RowMapper() {
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
return resultSet.getString("city_desc");
}
});
return branchList;
现在我希望能够将准备好的语句与查询一起使用,例如“从branchTable中选择分支,其中branch_name = ?”
如何使用 jdbcTemplate 做到这一点? 我搜索的示例显示了如何将其与更新或插入查询一起使用,但不与选择查询一起使用。
请帮忙。
最佳答案
JdbcTemplate 有 another query() method它将准备好的语句的参数作为参数:
jdbcTemplateObject.query(SQL, new Object[] {branchName}, new RowMapper() {...});
请注意:
-
SQL
应命名为sql
- 您应该使用
List
而不是ArrayList
。 javadoc 中没有任何内容保证返回 ArrayList。而且您不应该关心返回的列表的具体类型。 - 您应该使用
RowMapper<String>
而不是原始的RowMapper
.
关于spring - 如何在 spring mvc 3 中使用 jdbcTemplate 返回结果集的预准备语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19489978/