gpt4 book ai didi

java - JDBCTemplate 传递带/多个参数

转载 作者:行者123 更新时间:2023-11-29 21:39:09 25 4
gpt4 key购买 nike

事情是这样的...我无法通过以下代码传递以下语句“this”永远不会被打印,因此结果集为 0,但查询似乎是正确的。

查询:

select * from opportunities where title = 1 and (zipcode = 11738 or zipcode = 11720 or zipcode = 11727 or zipcode = 11741 or zipcode = 11742 or zipcode = 11755 or zipcode = 11763 or zipcode = 11776 or zipcode = 11779 or zipcode = 11784 or zipcode = 11953)


上面的查询确实返回结果。***

代码(只是切换了标题和邮政编码位置,运行代码时仍然会返回0结果)

public Opportunity[] getOpportunitiesBy(String title, String zipcode, double miles) {
title = ""+Constants.TITLES_MAP.get(title.toLowerCase());
String[] nearbyZipcodes = getZipcodesWithinRadius(zipcode, miles);
StringBuilder builder = new StringBuilder();
builder.append("(zipcode = "+zipcode+" or zipcode = ");
for(String otherZips : nearbyZipcodes) {
builder.append(otherZips+" or zipcode = ");
}
String formattedZips = Utilities.replaceLast(builder.toString(), " or zipcode = ", ")");
System.out.println(title+","+formattedZips);
List<Opportunity> opportunities = this.jdbcTemplate.query("select * from opportunities where ? and title = ?",
new Object[] { formattedZips, title}, new RowMapper<Opportunity>() {
public Opportunity mapRow(ResultSet rs, int rowNum) throws SQLException {
Opportunity temp = new Opportunity();
System.out.println("this");
String[] candidateIds = rs.getString("candidateIds").split(",");
temp.setCandidateIds(Utilities.StringToIntArray(candidateIds));
temp.setCompany(rs.getString("company"));
temp.setId(rs.getLong("id"));
temp.setHtml(rs.getString("post_data"));
temp.setZipcode(rs.getString("zipcode"));
temp.setTitle(rs.getInt("title"));
try {
temp.setLogoImg(new URI(rs.getString("logo_img")));
} catch (Exception e) {
}
return temp;
}
});
return opportunities.toArray(new Opportunity[opportunities.size()]);
}

初始 println(title+","+formattedZips) 的输出

1,(zipcode = 11738 or zipcode = 11720 or zipcode = 11727 or zipcode = 11741 or zipcode = 11742 or zipcode = 11755 or zipcode = 11763 or zipcode = 11776 or zipcode = 11779 or zipcode = 11784 or zipcode = 11953)

最佳答案

您的设置有两处错误。

首先,您不应该使用串联来创建(部分)查询,其次,这不是参数化查询的工作方式。参数在放入之前会被转义,所以我怀疑查询是否是您所期望的。

SQL 有 in 子句,而不是执行 zipcode 或 zipcode 或 zipcode),而是在查询中使用单个 in 子句。然而,当你想传递一个数组时,你又遇到了问题。要解决此问题,请使用 NamedParameterJdbcTemplate而不是普通的 JdbcTemplate。然后重写查询以使用命名参数和 in 子句。

public Opportunity[] getOpportunitiesBy(String title, String zipcode, double miles) {
String sql = "select * from opportunities where title = :title and zipcode in (:zips)";
title = ""+Constants.TITLES_MAP.get(title.toLowerCase());
String[] nearbyZipcodes = getZipcodesWithinRadius(zipcode, miles);

Map<String, Object> params = new HashMap<>();
params.put("title", nearbyZipcodes);
params.put("zips", near)

return this.jdbcTemplate.query(sql, params, new RowMapper<Opportunity>() {
public Opportunity mapRow(ResultSet rs, int rowNum) throws SQLException {
Opportunity temp = new Opportunity();
System.out.println("this");
String[] candidateIds = rs.getString("candidateIds").split(",");
temp.setCandidateIds(Utilities.StringToIntArray(candidateIds));
temp.setCompany(rs.getString("company"));
temp.setId(rs.getLong("id"));
temp.setHtml(rs.getString("post_data"));
temp.setZipcode(rs.getString("zipcode"));
temp.setTitle(rs.getInt("title"));
try {
temp.setLogoImg(new URI(rs.getString("logo_img")));
} catch (Exception e) {
}
return temp;
});

}

类似的东西应该可以解决问题,但是如果您的 getZipCodesWithinRadius 也使用查询,您甚至可以将其用作 in 子句中的子选择并简单地传递给定的查询,这可能会更容易zipcodemiles,这样您就可以通过一个查询一次性获得结果(而不是 2 个查询以及随之而来的所有 jdbc 内容)。

关于java - JDBCTemplate 传递带/多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com