0

I'm trying to create a for cycle with a dynamic sql statement:

for(int i = 0; i < size; i++) {         
            ps2 = db.prepareStatement("select * from student where id=?");
            ps2.setInt(1,studList[i]);
            rs2=ps2.executeQuery();
            while(rs2.next()){
                list1.add(new Student(rs2.getInt("id"), rs2.getString("name"), rs2.getString("city"));
            }
            rs2.close();
            ps2.close();
        }

studList[i] is an array with all the student ID and the variable "size" is the length of this array. The problem with this code is that only one element is added to the list(corresponding to the first student ID). I noticed that the code goes inside 'while(rs2.next())' just if the value of studList[i] doesn't change. Why?

5
  • 1
    put rs2.close(); ps2.close(); outside of for-loop and check. Commented Feb 7, 2019 at 11:11
  • @dkb same result Commented Feb 7, 2019 at 11:14
  • change while(rs2.next()){ with while(rs2.hasNext()){, and at the end of while loop rs2 = rs2.next();, this is what I Mean: stackoverflow.com/a/867206/2987755 Commented Feb 7, 2019 at 11:24
  • @dkb hasNext() return 'cannot find symbol' error Commented Feb 7, 2019 at 11:29
  • Try this way: stackoverflow.com/a/6813771/2987755 Commented Feb 7, 2019 at 11:30

1 Answer 1

3

Seemingly the logic is fine.

First the code would be better with

  • local declarations at the first usage, otherwise ps2, rs2, ps3, rs3 and so one risk undetected typos;
  • try-with-resources that close even on exception/return.

So:

String sql = "select name, city from student where id=?";
try (PreparedStatement ps2 = db.prepareStatement(sql)) {
    for (int i = 0; i < size; i++) {  
        int id = studList[i];
        ps2.setInt(1, id);
        try (ResultSet rs2 = ps2.executeQuery()) {
            System.out.printf("[%d] %d%n", i, id);
            while (rs2.next()) {
                System.out.printf("- %s%n", rs2.getString("name"));
                list1.add(new Student(id, rs2.getString("name"), rs2.getString("city"));
            }
        }
    }
}

So the error might happen before: the array filled false, the database student table corrupted.

One beginner error is to reuse an object (Student), with new field values. Adding the same object to a list then would have the last student's data replicated several times.

Here it could be that in the original code code was called reusing ps2 or such.

Sign up to request clarification or add additional context in comments.

7 Comments

same error, just one time inside 'while (rs2.next())'
One time in the while is okay (could have been if), as id is the primary key.
but is not the result that i'm expecting, the list should have 2 entries but it has just 1
And the second id is in the database?
(And new Student does not do beginner's error like static fields.) The printfs indeed show [0]... / - ... / [1] ... / nothing. Then it must be the database or the code shown is not the code used. You principly had correct code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.