从java中的预准备语句中获取值

时间:2015-07-30 19:42:38

标签: java sql

我需要一些准备好的声明帮助。我希望转换地图中的值与之前2个地图的总和相同。请给我一些帮助

 statement = connection.prepareStatement("select count(*) as count,b.bankid,status from................... between ? and ? group by status,bankid order by bankid");
                statement.setInt(1, PaymentType.CONVERSION);
                statement.setTimestamp(2, dateFrom);
                statement.setTimestamp(3, dateTo);
                rs = statement.executeQuery();
                while (rs.next()) {
                    int status = rs.getInt("status");
                    String bankid =rs.getString("bankid");

                    if (status == 1) {
                        suc.put(bankid, rs.getInt("count"));
                    }else {
                        fail.put(bankid, rs.getInt("count"));
                    }
                    conv.put(bankid, success.get("count") + failed.get("count"));

1 个答案:

答案 0 :(得分:0)

您使用了错误的密钥来显示successfailed地图。

success.get("count") + failed.get("count")

应该是

success.get(bankid) + failed.get(bankid)

如果这些地图当时没有该bankid的条目,您还应该防止从getsuccess地图failed返回空值你评估那个表达。

例如:

public Integer safeGet(Map<String, Integer> map, String key) {
  if (map == null || key == null || !map.containsKey(key)) {
    return Integer.valueOf(0);
  }
  return map.get(key);
}

然后使用:

conversion.put(bankid, safeGet(success, bankid) + safeGet(failed, bankid));