我只需要从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() 方法中指定的所有行。怎么做?
使用列表集合将 Map 键/值对添加为
List<LinkedHashMap<String,String>>l=new ArrayList<LinkedHashMap<String,String>>();
l.add(map);
然后返回列表变量 l。
resultset.absolute() --> 将光标移动到此 ResultSet 对象中的给定行号。
执行 while 循环后,结果集中的游标指向最后一行。结果集按顺序移动光标,直到调用 absolute 为止。因此,您总是得到最后一个元素。