0

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);

    }
A.Bukhari
  • 1
  • 1
  • You need to post `etudiant`; when you say `v.add(etudiant);` you don't appear to have created an instance in your loop.... this code looks specious. – Elliott Frisch Mar 10 '17 at 22:55
  • Please, respect the Java naming conventions, and don't use Vector. It's obsolete since Java 2. We're at Java 8. Use an ArrayList. – JB Nizet Mar 10 '17 at 22:56
  • 1
    Everytime the loop executes, you simply replace the values of etudiant object with the new values in the vector. You have to allocate a new **etudiant object** everytime the loop iterates. – ShayHaned Mar 10 '17 at 22:59
  • @ElliottFrisch , I created the object 'etudiant' right before entering the loop – A.Bukhari Mar 10 '17 at 23:02
  • @ShayHaned, I don't understand your comment, I've written the loop so that at each iteration, the object receives new values, and this object is put in a new cell, isn't that what this code does? – A.Bukhari Mar 10 '17 at 23:04
  • you have to create the object 'etudiant' inside the loop – Dilshod Shaymanov Mar 10 '17 at 23:05
  • @A.Bukhari Yes, it receives new values, but looses the previous one. So inside your loop before assigning new values, just say **etudiant = new etudiant()**, and then assign the values, that's exactly what ElliotFrisch pointed out – ShayHaned Mar 10 '17 at 23:06
  • Yes it works fine now,thanks a lot, but could you please tell me the mistake in declaring 'etudiant' outside the loop?I thought 'etudiant' stores its values in a cell before it receives new ones... – A.Bukhari Mar 10 '17 at 23:09
  • Actually, I understand now, I was filling the entire array with the same object, so each time I modify the object, the whole array receives the new values. Interresting! Thanks a lot guys! – A.Bukhari Mar 10 '17 at 23:14
  • @A.Bukhari Great!, You got it now!! – ShayHaned Mar 10 '17 at 23:15

0 Answers0