Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset the result set to the first row after looping through a while loop

Tags:

java

resultset

My code has two loops, my outer loop should loop through all the rows of the inner loop for the first row of outer loop, and the for the second row of the outer loop, it should loop through all the rows 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++;

}
like image 225
Anusha Avatar asked Aug 07 '15 15:08

Anusha


People also ask

How do you set ResultSet to first record?

The ResultSet object contains a cursor/pointer which points to the current row. Initially this cursor is positioned before first row (default position). You can move the cursor of the ResultSet object to the first row from the current position, using the first() method of the ResultSet interface.

Which method moves the cursor to the beginning of the result set that is before first row?

beforeFirst : Positions the cursor at the start of the ResultSet object, before the first row. If the ResultSet object does not contain any rows, this method has no effect. afterLast : Positions the cursor at the end of the ResultSet object, after the last row.

How do you iterate through ResultSet?

To iterate the ResultSet you use its next() method. The next() method returns true if the ResultSet has a next record, and moves the ResultSet to point to the next record. If there were no more records, next() returns false, and you can no longer.

Which method is used to return a ResultSet?

In the Following JDBC example the retrieveData() method establishes a connection with the database and retrieve the contents of the table MyPlayers in to a ResultSet object and returns it. The main method invokes the retrieveData() method and prints the contents of the obtained ResultSet.


2 Answers

Use this:

resultSet.beforeFirst()

From docs:

Throws: SQLException - if a database access error occurs; this method is called on a closed result set or the result set type is TYPE_FORWARD_ONLY

like image 199
ka4eli Avatar answered Oct 06 '22 20:10

ka4eli


Use

resultSet.beforeFirst();

But you have to create the Statement this way:

Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

So that it is traversable and not one direction only.

like image 43
Renis1235 Avatar answered Oct 06 '22 19:10

Renis1235