Raghu
Raghu

Reputation: 1161

Get 2 consecutive rows of a resultset

I need to compare a row of a ResultSet with the consecutive row if the row string matches a particular string.

   while(rs.next())
    {
       String name = rs.getString("name");
     if(name.equalsIgnoreCase("SomeName")
      {
       String nextName =    //code to get the next consecutive row
        if(nextName.contains(name)
        {
         name = "NA";
         }

      }
     stringList.add(name);
     }

How can I get the next row while the cursor is still on that row?

Upvotes: 1

Views: 714

Answers (4)

Adriano Carneiro
Adriano Carneiro

Reputation: 58595

Approach hint: try storing the first one in a variable and then compare when reading the next row.

Really teaching you how to fish here, not handing you the catch.

Upvotes: 2

Reimeus
Reimeus

Reputation: 159754

To flesh this out (a little):

String lastName = null;
while (rs.next()) {
    // do stuff with this row
    if (name.equalsIgnoreCase("SomeName") && lastName != null) {
        // work with lastName & SomeName
    }

    // save/assign lastName
}

Upvotes: 1

IConfused
IConfused

Reputation: 724

Hint: Try to store first row value then use another loop and compare that value. Hint: use nested loop

Upvotes: 0

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

You can use rs.next(), rs.getString() and then rs.previous() to move one row forward.

However a better option would be to memorize previous string value in a local variable and compare it at the next iteration.

Upvotes: 0

Related Questions