I am trying to iterate over the resultset from a query but I am getting some weird behaviour I can not explain. I've been searching online and offline for a few hours now and I hope one of you can see what is causing it or might know what could cause it.
I am using Maven, JEE and JDBC.
After running a query against a MySQL database I am trying to iterate over it and add a DTO (Data Transfer Object) to a list.
class PersonDTO {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The class in which I iterate over the results injects PersonDTO using CDI.
I can make a query without any problem but is goes wrong here (rs is the ResulSet:
List<PersonDTO> persons = new ArrayList<>();
while(rs.next())
String name = rs.getString("name");
System.out.println("Current name: " + name);
PersonDTO.setName(name);
persons.add(playlistDTO);
}
My console shows the correct result:
name: Jack
name: Doris
However in the persons array the content is Doris, Doris (The second name twice).