1

I have this request :

private List<IWsResponse> getBPartnerDetails(String valueNetwork, String reportLevel2) {
        JdbcTemplate tm = new JdbcTemplate(ds.getDataSource());
        StringBuffer sql =  new StringBuffer("SELECT * FROM XRV_BPARTNERDETAILS where rownum < 10 order by BPartner_ID");
        response = tm.query(sql.toString(),  new BPartnerMapper());
        return response;
    }

in the BPartnerMapper, i want to do the following :

get the previous elements in the ResultSet to compare it with actual line

@Override
    public IWsResponse mapRow(ResultSet rs, int rowNum) throws SQLException {
        rs.previous();
        int prev_Bpartner_ID = rs.getInt("BPARTNER_ID");
        int prev_Location_ID = rs.getInt("BPARTNER_LOCATION_ID");
        int prev_User_ID = rs.getInt("User_ID");
        rs.next();
        int Bpartner_ID = rs.getInt("BPARTNER_ID");
        int Location_ID = rs.getInt("BPARTNER_LOCATION_ID");
        int User_ID = rs.getInt("User_ID");
        // My code
    }

I get the error in rs.previous() :

java.sql.SQLException: Invalid operation for forward only resultset : previous

how can i fix that to get the previous elements of resultSet

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
mugiwaradz
  • 393
  • 1
  • 3
  • 15
  • Post the code where you are creating the `Statement` – Rahman Mar 30 '16 at 09:57
  • You shouldn't be doing `next()` or `previous()` in the first place in a `RowMapper`. You should use a `ResultSetExtractor` and simply iterate over the results (keep the last one and compare with the next one). Instead of going back and forward. You should also not create a `JdbcTemplate` each time you need one, performance wise not very smart to do as it is a heavy object to construct. – M. Deinum Mar 30 '16 at 10:33
  • What is your use case, why do you need to do this comparison? – M. Deinum Mar 30 '16 at 10:33

1 Answers1

0

You can't call prev() or next() on the ResultSet as it pre-initialized for the current row.

From the Spring Documentation of mapRow():

Implementations must implement this method to map each row of data in the ResultSet. This method should not call next() on the ResultSet; it is only supposed to map values of the current row.

What you should be doing instead is:

  1. Get an ArrayList out of your query().
  2. Iterate this list and do the needed comparisons.
user2004685
  • 9,548
  • 5
  • 37
  • 54