遍历while循环后如何将结果集重置为第一行 [英] how to reset the result set to the first row after looping through a while loop

查看:60
本文介绍了遍历while循环后如何将结果集重置为第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有两个循环,我的外循环应该循环内循环的所有行作为外循环的第一行,外循环的第二行应该循环内循环的所有行内排.

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

}

推荐答案

使用这个:

resultSet.beforeFirst()

来自文档:

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

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

这篇关于遍历while循环后如何将结果集重置为第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆