Why can't I read the following lines in a ResultSet

I have a problem. I am trying to read another line in my ResultSet, but dunno why my loop does not end with rs.next (). Yes, I got more than 1 line in tblPracownicy. I check the contents of the ResultSet from while (rs.next ()) system.out.println (rs.getRow (1)) to DO, and it looks good, I got as many results as I got rows in tblPracownicy. But in the DO loop, I did not know why it would not be a loop cycle. 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()); 
+4
source share
2 answers

ok the problem is solved, two requests cause "interference" in the ResultSet operation.

 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]); 
+1
source

You should use a regular while , not a do ... while .

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

If you use a do ... while , the do ... while body is executed first, and then the condition is checked. What will fail if you do not get any results from the database.

Remember that calling ResultSet.next() does not just check the condition. It actually moves the cursor. Something like how Iterator.next() works.

So, in the first iteration, when do block is executed, the cursor doesn’t point anywhere (it hasn’t advanced yet since you did not call next() on it), which causes the behavior that you see.

+3
source

Source: https://habr.com/ru/post/1396185/


All Articles