这是事情......我无法通过以下代码传递以下语句“此”从不打印,因此结果集为0但查询似乎正确。
查询:
从title = 1和(zipcode = 11738或zipcode = 11720或zipcode = 11727或zipcode = 11741或zipcode = 11742或zipcode = 11755或zipcode = 11763或zipcode = 11776或zipcode = 11779或zipcode的机会中选择* = 11784或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的输出(标题+“,”+ formattedZips)
1,(zipcode = 11738或zipcode = 11720或zipcode = 11727或zipcode = 11741或zipcode = 11742或zipcode = 11755或zipcode = 11763或zipcode = 11776或zipcode = 11779或zipcode = 11784或zipcode = 11953)
答案 0 :(得分:0)
您的设置有两个问题。
首先,您不应该使用连接来创建(部分)查询,其次不是参数化查询的工作方式。参数在放入之前进行了转义,因此我怀疑查询是否符合您的预期。
SQL具有in
子句而不是zipcode or zipcode or 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子句中的子选择并简单地传递给定{{1}和zipcode
一样,你会有一个查询一次性得到结果(而不是2个查询和所有jdbc的东西)。