java - Iterating over JDBC result set repeating result on last iteration -


as title says, here code:

string query = "select * " + table_university_info + " university_code = ?";     connection conn = drivermanager.getconnection(database_schema, username, password);      preparedstatement stmt = conn.preparestatement(query);     stmt.setstring(1, universitycode);     resultset rs = stmt.executequery();     while (rs.next()) {             // dto database             summoneruniversitydto.setuniversityname(rs.getstring("university_code"));             summoneruniversitydto.setid(rs.getlong("summonerid"));             summoneruniversitydto.setsummonername(rs.getstring("summoner_name"));              //add array             summoneruniversitydtoarray.add(summoneruniversitydto);             logger.info("array" + summoneruniversitydtoarray);             count++;         }         logger.info(string.format("[%s] summoners returned database matching university code [%s]", count, universitycode));         universitysummonerdto.setsinglesummonerplayerdtos(summoneruniversitydtoarray);         logger.info(string.format("universityarraydto [%s]", universitysummonerdto)); 

in database have 3 rows returned, each 1 different name , uid - ripolly, kaibai , lol.

it's getting of results, when hit endpoint , @ what's being returned:

2016-01-24 18:25:31 info  databaseaccessor:201 - returning university dto g iven university name: [some]  2016-01-24 18:25:31 info  databaseaccessor:219 - array[summoneruniversitydto{sum monerid=155767, summonername='lol', universityname='some'}]  2016-01-24 18:25:31 info  databaseaccessor:219 - array[summoneruniversitydto{sum monerid=37656713, summonername='kaibai', universityname='some'}, summonerunivers itydto{summonerid=37656713, summonername='kaibai', universityname='some'}]  2016-01-24 18:25:31 info  databaseaccessor:219 - array[summoneruniversitydto{sum monerid=38584682, summonername='ripolly', universityname='some'}, summoneruniver sitydto{summonerid=38584682, summonername='ripolly', universityname='some'}, sum moneruniversitydto{summonerid=38584682, summonername='ripolly', universityname=' some'}]  2016-01-24 18:25:31 info  databaseaccessor:222 - [3] summoners returned fro m database matching university code [some]  2016-01-24 18:25:31 info  databaseaccessor:224 - universityarraydto [universi tysummonerdto{singlesummonerplayerdtos=[summoneruniversitydto{summonerid=3858468 2, summonername='ripolly', universityname='some'}, summoneruniversitydto{summone rid=38584682, summonername='ripolly', universityname='some'}, summoneruniversity dto{summonerid=38584682, summonername='ripolly', universityname='some'}]}] 

the names entered fine array, being repeated in arraylist. problem code, or way i'm iterating on it/result set cursors etc. stuck on while!

apologies naming conventions aren't simple...

cheers

you aren't creating new object in each iteration of while loop, therefore it's editing same object in array , adding 2nd , 3rd time.

the while loop should like:

while (rs.next()) {         summoneruniversitydto = new summoneruniversitydto();         // dto database         summoneruniversitydto.setuniversityname(rs.getstring("university_code"));         summoneruniversitydto.setid(rs.getlong("summonerid"));         summoneruniversitydto.setsummonername(rs.getstring("summoner_name"));          //add array         summoneruniversitydtoarray.add(summoneruniversitydto);         logger.info("array" + summoneruniversitydtoarray);         count++;     } 

Comments