【发布时间】:2016-01-08 12:11:31
【问题描述】:
事情是这样的……我无法通过以下代码传递以下语句“this”从未打印,因此结果集为 0,但查询似乎正确。
查询:
从 title = 1 和 (zipcode = 11738 或 zipcode = 11720 或 zipcode = 11727 或 zipcode = 11741 或 zipcode = 11742 或 zipcode = 11755 或 zipcode = 11763 或 zipcode = 11776 或 zipcode = 11779 或 zipcode 的机会中选择 * = 11784 或邮政编码 = 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,(邮编 = 11738 或邮编 = 11720 或邮编 = 11727 或邮编 = 11741 或邮编 = 11742 或邮编 = 11755 或邮编 = 11763 或邮编 = 11776 或邮编 = 11779 或邮编 = 11784 或邮编 = 11953)
【问题讨论】:
标签: java mysql spring jdbc spring-jdbc