如何使用ResultSet.absolute()方法从java中的LinkedHashMap中检索所有值

时间:2015-02-02 09:24:07

标签: java resultset linkedhashmap

我必须从ResultSet中只检索一些特定的行,因为我有ResultSet的absolute()方法,并将该行的值放在LinkedHashMap中。但是当我执行代码时,只打印最后一行而不是所有指定的行。 代码是:

public LinkedHashMap <Date, Double> reference() {
  int rowCounter = 0;


  String a[][] = new String[46][2];
  int i = 0;
  try {
    con = getConnection();
    stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    String sql = "select logtime,beam_current from INDUS2_BDS.dbo.DCCT where logtime between '2014-10-10 07:17:00' and '2014-10-10 08:46:00'" +
      "and (beam_current like '%9.96' or beam_current like '%9.97' or beam_current like '%9.98' or  beam_current like '%9.99'  or beam_current like '%0' or beam_current like '%_0.01' or beam_current like '%_0.02' or beam_current like '%_0.03' or beam_current like '%_0.04' or beam_current like '%_0.05' or beam_current like '%_0.06')";
    stmt.executeQuery(sql);
    rs = stmt.getResultSet();



    while (rs.next()) {

      for (int j = 0; j < 2; j++)

      {

        a[i][j] = rs.getString(j + 1);
      }

      i++;
      rowCounter++;

      System.out.println(rowCounter);
      if (rowCounter == 4 || rowCounter == 9 || rowCounter == 11 || rowCounter == 13 || rowCounter == 15)
        rs.absolute(4);
      map.put(rs.getDate(1), (double) rs.getFloat(2));
      rs.absolute(9);
      map.put(rs.getDate(1), rs.getDouble(2));
      rs.absolute(11);
      map.put(rs.getDate(1), rs.getDouble(2));
      rs.absolute(13);
      map.put(rs.getDate(1), rs.getDouble(2));
      rs.absolute(15);
      map.put(rs.getDate(1), rs.getDouble(2));
      rs.absolute(16);
      map.put(rs.getDate(1), rs.getDouble(2));
      rs.absolute(18);
      map.put(rs.getDate(1), rs.getDouble(2));

    }
  }


} catch (Exception e) {
  System.out.println("\nException " + e);
} finally {
  closeConnection(stmt, rs, con);
}

return map;

我希望检索结果集的absolute()方法中指定的所有行。怎么做?

2 个答案:

答案 0 :(得分:1)

使用列表集将地图键/值对添加为

List<LinkedHashMap<String,String>>l=new ArrayList<LinkedHashMap<String,String>>();  
l.add(map);

然后返回列表变量l。

答案 1 :(得分:0)

resultset.absolute() - &gt;将光标移动到此ResultSet对象中的给定行号。

执行while循环后,结果集中的光标指向最后一行。结果集按顺序移动光标,直到您调用绝对值。 因此,你总是得到最后一个元素。