How to add objects fetched from database to arraylist?

I wrote a simple program to fetch some data from a database, each row from a database table is structured in an object that I create using a normal constructor and then add that object to my array using the .add () method. my problem is that when I print out what my arraylist contains, I find all cases containing only the last row from the database !! i tried to print by adding to the arraylist, i found out that every time i add a new object, it gets in the new and old case of the arraylist, so the last object (which represents the last row of the database table) gets in all archaists! so any help plz?

here are my two classes: Client.java

public class Client {
    public static String nom, prenom, adrs;
    private static int id, maxcredit, totalpaye, totalnonpaye;

    //i want data to be stored in this arraylist
    public static ArrayList<ClientInfo> clients =  new ArrayList();

public Client(){    
    connectDB();//connecting to the database
    getClients();//storing the data from the database to arraylist!!
}

private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;

private static final String Conn_string = "jdbc:mysql://localhost/carnetcredit";



//Connect to DB:
public static void connectDB(){
    try{
        conn = DriverManager.getConnection(Conn_string, "root", "");
    }catch(SQLException e){
        System.err.println(e);
    }
}

//Get all clients list:
public static void getClients(){
    try{

        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery("select * from client");

        System.out.println("Table Client : ");

        while (rs.next()){

            //getting data from database to simpte variables
            id = rs.getInt("idclient");
            nom = rs.getString(2);
            prenom = rs.getString(3);
            adrs = rs.getString(4);
            maxcredit = rs.getInt(5);
            totalpaye = rs.getInt(6);
            totalnonpaye = rs.getInt(7);

            //creating an object using the data extracted from the database
            ClientInfo client = new ClientInfo(id,nom,prenom,adrs,maxcredit,totalpaye,totalnonpaye);

            //adding the object to the arraylist
            clients.add(client);

    }

    }catch(SQLException e){
        System.err.println(e);
    }

}


//::::::::::::::::::::::::::::::::::::::::::::::::::: Main Method :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public static void main(String[] args) {

    conn = null;
    stmt = null;
    rs = null;


    ClientInfo client = new ClientInfo();
    new Client();
    int i = 0;
    while (i<clients.size()){
        client = clients.get(i);
        System.out.println("id : "+ client.id +" - nom : "+client.nom+" - prenom : "+client.prenom+" - adresse : "+client.adrs+
                " - maxcredit : "+client.maxcredit+" - total payé : "+client.totalpaye+" - total non payé : "+client.totalnonpaye);
        i++;
    }
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

}

      

ClientInfo.java

public class ClientInfo {

    public  String nom, prenom, adrs;
    public  int id, maxcredit, totalpaye, totalnonpaye;


public ClientInfo(){
    id = 0;
    nom = prenom = adrs = "";
    maxcredit = totalpaye = totalnonpaye = 0;
}

public ClientInfo(int id, String nom, String prenom, String adrs, int maxcredit, int totalpaye,int totalnonpaye){
    this.id = id;
    this.nom = nom;
    this.prenom = prenom;
    this.adrs = adrs ;
    this.maxcredit = maxcredit ;
    this.totalpaye = totalpaye ;
    this.totalnonpaye = totalnonpaye;

}


  }

      

Thanks guys!

+3


source to share


1 answer


Whenever I see

containing only the last row from the database

I automatically know this is caused by static variables.



change

public static String nom, prenom, adrs;
private static int id, maxcredit, totalpaye, totalnonpaye;

      

non-static

+6


source







All Articles