java 遍历while循环后如何将结果集重置为第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31880940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:17:40  来源:igfitidea点击:

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

javaresultset

提问by Anusha

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++;

}

回答by ka4eli

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

抛出: SQLException - 如果发生数据库访问错误;在关闭的结果集或结果集类型为 TYPE_FORWARD_ONLY 上调用此方法

回答by cbender

Here's what you're looking for:

这是您要查找的内容:

rm.beforeFirst()

Official documentation:

官方文档

Moves the cursor to the front of this ResultSet object, just before the first row. This method has no effect if the result set contains no rows.

将光标移动到此 ResultSet 对象的前面,就在第一行之前。如果结果集不包含行,则此方法无效。