简体   繁体   English

为什么我无法读取ResultSet中的下一行

[英]Why i can't read the next rows in ResultSet

I have a problem i'm trying to read another row in my ResultSet but dunno why can't my loop just end at rs.next(). 我有一个问题,我正在尝试读取我的ResultSet中的另一行,但不知道为什么我的循环不能在rs.next()结束。 Yes, i got more than 1 rows in tblPracownicy. 是的,我在tblPracownicy中获得了超过1行。 I check ResultSet content with while(rs.next())system.out.println(rs.getRow(1)) before DO and it look good i got that number of result as much i got rows in tblPracownicy. 我在DO之前用while(rs.next())system.out.println(rs.getRow(1))检查ResultSet内容,它看起来很好我得到了那个数量的结果,因为我在tblPracownicy中得到了行。 But in DO loop i didn't know why it won't cycle the loop. 但在DO循环中,我不知道它为什么不循环循环。 here is the code: 这是代码:

ResultSet rs2 = stat.executeQuery("select _id, cykl_id, CyklDzien, CyklData, DataZatrudnienia, DataZwolnienia from tblPracownicy");
            //wygeneruj harmonogramy dla pracowników
            long prac_id = rs2.getLong(1);
            int offset;
            Cykl cykl = null;

            do
            { //pracownicy
                //odnajdź i pobierz cykl dla pracownika prac_id
                if (cykl == null || cykl.cykl_id != rs2.getLong(2))
                    for (Cykl c : cykle)
                        if (c.cykl_id == rs2.getLong(2)) 
                        {
                            cykl = c;
                            break;
                        }
                //ustaw offset cyklu na dzień 1.szy
                GregorianCalendar gc_cykl = new GregorianCalendar();
                gc_cykl.setTime(rs2.getDate(4));
                gc_harm = new GregorianCalendar(rok, miesiac-1, 1);
                //uwzględnij startowy dzień cyklu = CyklDzien
                gc_cykl.add(GregorianCalendar.DAY_OF_MONTH, -rs2.getInt(3)+1);
                offset = (int) ((gc_harm.getTimeInMillis() - gc_cykl.getTimeInMillis()) / (3600000*24)) % cykl.dlugosc;
                //przelicz offset na dodatni
                if (offset < 0) offset = cykl.dlugosc + offset;

                GregorianCalendar gc_zatr = new GregorianCalendar(new Integer(rs2.getString(5).substring(0, 4)), new Integer(rs2.getString(5).substring(5, 7)), new Integer(rs2.getString(5).substring(8)));
                GregorianCalendar gc_zwol = null;
                //if (!(rs2.getString(6) == null || rs2.getString(6).equals("")))
                if ((rs2.getString(6) != null && !rs2.getString(6).equals("")))
                {
                    gc_zwol = new GregorianCalendar(new Integer(rs2.getString(6).substring(0, 4)), new Integer(rs2.getString(6).substring(5, 7)), new Integer(rs2.getString(6).substring(8)));
                }
                //definiuj zmiany pracownika na cały miesiąc
                for (int dzien=1; dzien <= max; dzien++)
                { //dni
                    //w dni miesiąca kiedy pracownik nie jest zatrudniony wstawić dni wolne
                    gc_harm.set(rok, miesiac, dzien);
                    if (gc_harm.before(gc_zatr) || (gc_zwol != null && gc_harm.after(gc_zwol)))  //jesli przed zatrudnieniem lub po zwolnieniu
                    {   //wpisz dzien wolny = niekasowalne godziny 'xx - xx' o id = 0
                        stat.executeUpdate("insert into tblZmiany (Harmonogram_id, dzien, pracownik_id, godziny_id) "
                                + "values (" + id + ", " + dzien + ", " + prac_id + ", " + 0 + ");");
                    }else{
                        //wpisz zmianę
                        stat.executeUpdate("insert into tblZmiany (Harmonogram_id, dzien, pracownik_id, godziny_id) "
                                + "values (" + id + ", " + dzien + ", " + prac_id + ", " + cykl.godziny[offset] + ");");
                    }
                    offset++;
                    if (offset >= cykl.dlugosc) offset = 0;
                }
            }while (rs2.next());

You should use a normal while loop, not a do ... while one. 您应该使用正常的while循环,而不是do ... while一个。

while(rs2.next()) {
    // do your stuff
}

If you use a do ... while loop, the body of the loop gets executed first and then the condition gets checked. 如果使用do ... while循环,则首先执行循环体,然后检查条件。 Which will fail if you don't get any results back from the database. 如果您没有从数据库中获得任何结果,那将失败。

Bear in mind that the ResultSet.next() call won't just check the condition. 请记住, ResultSet.next()调用不仅会检查条件。 It actually advances the cursor. 它实际上推进了光标。 Something like how the Iterator.next() works. Iterator.next()工作原理。

So, on the first iteration when the do block executes, the cursor is pointing nowhere (it hasn't yet advanced as you haven't invoked the next() on it) which causes the behaviour you're seeing. 因此,在do block执行的第一次迭代中,光标无处指向(它尚未提升,因为你没有调用它上面的next() ),这会导致你看到的行为。

ok problem solved, the two query cause "interference" in working of ResultSet. ok问题解决了,这两个查询导致ResultSet工作中的“干扰”。

if (gc_harm.before(gc_zatr) || (gc_zwol != null && gc_harm.after(gc_zwol)))  //jesli przed zatrudnieniem lub po zwolnieniu
                {   //wpisz dzien wolny = niekasowalne godziny 'xx - xx' o id = 0
                    stat.executeUpdate("insert into tblZmiany (Harmonogram_id, dzien, pracownik_id, godziny_id) "
                            + "values (" + id + ", " + dzien + ", " + prac_id + ", " + 0 + ");");
                }else{
                    //wpisz zmianę
                    stat.executeUpdate("insert into tblZmiany (Harmonogram_id, dzien, pracownik_id, godziny_id) "
                            + "values (" + id + ", " + dzien + ", " + prac_id + ", " + cykl.godziny[offset] + ");");
                }

Now i got: 现在我得到了:

if (gc_harm.before(gc_zatr) || (gc_zwol != null && gc_harm.after(gc_zwol)))  //jesli przed zatrudnieniem lub po zwolnieniu
                    {   //wpisz dzien wolny = niekasowalne godziny 'xx - xx' o id = 0
                        listaZapytan[x++] = "insert into tblZmiany (Harmonogram_id, dzien, pracownik_id, godziny_id) "
                                + "values (" + id + ", " + dzien + ", " + prac_id + ", " + 0 + ");";
                        System.out.println(listaZapytan[x-1]);

                    }else{
                        //wpisz zmianę
                        listaZapytan[x++] = "insert into tblZmiany (Harmonogram_id, dzien, pracownik_id, godziny_id) "
                                + "values (" + id + ", " + dzien + ", " + prac_id + ", " + cykl.godziny[offset] + ");";
                        System.out.println("cykl.godziny[" + offset + "] = " + cykl.godziny[offset]);
                        System.out.println(listaZapytan[x-1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM