Search code examples
javasqlresultset

How to capture value from rs.next() before looping through sql database?


SQL db

Refer the image.

I am new to Java.So in the table I want var x to point to first row.Initially var y should also point to first row.I want to just update y variable to next row in the while loop every time keeping x static until x and y matches.In the else part once value of y changes I need to update x to new value which matches y.How to do so?

var x= Initial;//dont know how to capture value over here.
while(rs.next){
var y=rs.getInt(1);
if(y==x){
 }
else{}

Solution

  • Try this :

        int x= 0;
        while(rs.next){
            if (x == 0){
                x=rs.getInt(1);
            }
            int  y = rs.getInt(1);
            if(y !=x){
                x=y;
            }
        }