I have to retrieve only some specific rows from the ResultSet ,for that I have absolute() method of ResultSet and have put the values of that row in a LinkedHashMap. But when I execute the code ,only last row is being printed not all the specified rows. The code is:
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;
I want all the rows specified in absolute() method of resultset to be retrieved. How to do that?
resultset.absolute() --> Moves the cursor to the given row number in this ResultSet object.
After the execution of while loop the cursor in result set points to last row. Result set moves the cursor sequentially until you call absolute. Because of this you are getting the last element always.