耗尽的异常结果集一次又一次地发生

时间:2013-11-30 04:21:08

标签: java sql resultset

结果集耗尽异常一次又一次地发生,我检查了所有可能性。我的表包含类似的数据,但它正在发生..我在这里粘贴我的代码..请指导我

  ArrayList<Duty> myList = new ArrayList<Duty>();
    try {

        String query = "select * from CBMS.DUTIES";
        ResultSet r = con.statement.executeQuery(query);

        while (r.next()) {
            Centre c = null;
            Teacher t = null;
            query = "select * from CBMS.center";
            ResultSet r1 = con.statement.executeQuery(query);

            while (r1.next()) {

                if (r.getInt(2) == r1.getInt(1)) {
                    c = new Centre(r1.getString(3), r1.getInt(1), r1.getString(2));
                    break;
                }
            }

            query = "select * from CBMS.teacher";
            ResultSet r7 = con.statement.executeQuery(query);
            while (r7.next()) {
                System.out.println(r7.getString(1));
                if (r.getInt(3) == r7.getInt(2)) {
                    t = new Teacher(r7.getString(1), r7.getInt(2), r7.getString(3), r7.getString(4), r7.getString(5));

                    break;
                }
            }


            Duty d = new Duty(c, t, r.getString(4), r.getInt(1));
            myList.add(d);
        }

    } catch (Exception e) {

        System.out.println(e.getMessage());
    }
    return myList;

1 个答案:

答案 0 :(得分:1)

使用常规结果集时,无法像在代码中那样来回移动。因此,当您运行r.next()时,您无法转到之前的值(并且您会多次执行此操作)。见this。您应该重构代码并将值放入可以重用的变量中。