Reputation: 18550
Im confused as when i add code inside a loop it stops the looping (effectivly ending after the first round).
Code which is working fine
StringBuilder sb = new StringBuilder(4096);
while ((rs.next())) {
System.err.println(rs.getRow());
sb.append(rs.getString(1)).append(",");
}
writeToCell(sheet,"P",row.getRowNum()+1,sb.toString());
Code which doesnt loop
StringBuilder sb = new StringBuilder(4096);
while ((rs.next())) {
System.err.println(rs.getRow());
String p=rs.getString(1);
ArrayList<Parent> pscore = new ArrayList();
pscore.add(new Parent(p));
String Score= getSegment(pscore);
sb.append(p).append("(").append(Score).append("),");
}
writeToCell(sheet,"P",row.getRowNum()+1,sb.toString());
I cant figure out why it would loop in the top version and not the bottom version. No errors are thrown
Upvotes: 0
Views: 118
Reputation: 533442
My guess is that
pscore.add(new Parent(p));
String Score= getSegment(pscore);
performs a database query which replaces the rs
which has finished before getSegment returns so the next call to rs.next()
will be false.
To avoid this I would make sure that rs
is a local variable instead of a field.
If this is the case, the best solution is likely to make the original SELECT a join between the tables so that is has all the information you need rather than performing a query per row.
Upvotes: 1
Reputation: 18550
Keppils comment drew me towards the answer
"Maybe something happens to rs in the getSegment() method? "
Im not sure how to give any credit ive upvoted the comment but cant accept the comment as correct
The Parent Class uses a static variable for the database connection so when its defined it overridden the rs variable.
Upvotes: 0