How to reset the result is set to the first line after the loop through a while loop

My code has two loops, my outer loop must go through all the lines of the inner loop for the first line of the outer loop, and for the second line of the outer loop, it must go through all the lines of the inner row.

int y1,y2;
float t = 0,s1,s2;

while(rm.next())
{
    int currentCol = 0;

    cellNumber = new jxl.write.Number (currentCol++, currentRow, index, integerFormat);
    index++;
    sheet.addCell ( cellNumber );

    cellLabel = new Label(currentCol++, currentRow, rs.getString("Name"));
    sheet.addCell ( cellLabel );


    y1 = rm.getInt("Year");

    System.out.println("Year 1: " +y1);


    cellNumber = new jxl.write.Number (currentCol++, currentRow, y1 , integerFormat);
    sheet.addCell ( cellNumber );



    s1 = rm.getFloat("Total_Price");
    System.out.println("S1: "+s1);

    while (rs.next())
    {
        y2 = rs.getInt("Year");
        System.out.println("Year 2: " +y2);

        s2 = rs.getFloat("Total_Price");
        System.out.println("S2: " +s2);

        if(y1==y2)
        {
            t = s1+s2;    
            System.out.println("Toatl Sales:" +t);

            cellNumber = new jxl.write.Number (currentCol++, currentRow, t , priceformat);
            sheet.addCell ( cellNumber );

        }


    } 
    int a = rs.getFetchDirection();
    System.out.println("Direction: " +a);

    rs.setFetchDirection(ResultSet.FETCH_REVERSE);


    cellNumber = new jxl.write.Number (currentCol++, currentRow, s1 , priceformat);
    sheet.addCell ( cellNumber );




    currentRow++;

}

      

+3


source to share


2 answers


Use this:

resultSet.beforeFirst()

      



From the docs:

Throws: SQLException - if a database access error occurs; this method call a closed result set or result set type TYPE_FORWARD_ONLY

+6


source


Here you are looking for:

rm.beforeFirst()

      



Official documentation :

Moves the cursor to the beginning of this ResultSet, just before the first line. This method has no effect if there are no rows in the result set.

+1


source







All Articles