I have a class called Technician
public class Technician {
private String empLName;
private String empFName;
private int empId;
//I skipped all setters and getters
}
In other class I retrieve all technicians names and load them into the array list.
Technician empl = new Technician();
ArrayList <Technician> employees = new ArrayList<Technician>();
//...skip code related to database
// rs is ResultSet
while (rs.next()){
empl.setEmpFName(rs.getString("EMP_LNAME"));
empl.setEmpLName(rs.getString("EMP_FNAME"));
empl.setEmpId(rs.getInt("EMP_ID"));
employees.add(empl);
}
When I debug I see correct values being retrieved from database. At first iteration of the while loop my empl object gets a value of the first employee in the database and it is being stored in employees ArrayList. At second iteration, the first object in employees ArrayList gets overwritten with the value of the second employee. Thus, I have two employees in my ArrayList with the same lastname , first name. At the third iteration, the same story, two employees in employees ArrayList are overwritten with value of the third employee from the database.
I would appreciate if any suggestions how to fix my code. Thanks,