I'm trying to read a table on a local Database through a Data Access Object . for each row, I want to create an object that contains the informations of the row. I used a vector inside the while(rs.next()) loop so that each iteration fills a cell with an object . But I get this error : the object in the last row is filling all the cells of the vector. I don't understand why this is happening , I thought using a vector would prevent overwriting elements
while(rs.next())
{
int ID=rs.getInt("ID");
String Nom=rs.getString("Nom");
String Prenom=rs.getString("Prenom");
int Age=rs.getInt("Age");
etudiant.setID(ID);
etudiant.setNom(Nom);
etudiant.setPrenom(Prenom);
etudiant.setAge(Age);
v.add(etudiant);
for(int i=0;i<v.size();i++)
{
etudiant etdd=v.elementAt(i);
String nom =etdd.getNom();
String prenom=etdd.getPrenom();
int age=etdd.getAge();
int id=etdd.getID();
}
}
here is the code I used to display the content of my vector (in my Main)
Vector<etudiant> vctr=DAO.GetEtudiants();
for(int i=0;i<vctr.size();i++)
{
etudiant etdd=vctr.elementAt(i);
String nom =etdd.getNom();
String prenom=etdd.getPrenom();
int age=etdd.getAge();
int id=etdd.getID();
System.out.println("ID: "+id+" |Nom: "+nom+" |Prenom: "+prenom+" |Age: "+age);
}