ArrayList doesn't store all elements from a database in Java

Cristina Moroti :

I have the following code. However, when I run it, I don't see in the JTextArea all the elements that are in my database, just the last one. But if I print out what I read from the database in that while loop, I get all of them, so I guess the problem is with the list of objects. Any suggestions of how I can solve this?

public class Muzica {
    static ArrayList <Melodii> lista=new ArrayList<Melodii>();
    static class Melodii
    {
        private String melodie;
        private String artist;
        private int an;
        public String getMelodie() {
            return melodie;
        }
        public void setMelodie(String melodie) {
            this.melodie = melodie;
        }
        public String getArtist() {
            return artist;
        }
        public void setArtist(String artist) {
            this.artist = artist;
        }
        public int getAn() {
            return an;
        }
        public void setAn(int an) {
            this.an = an;
        }

        public Melodii(String melodie, String artist, int an)
        {
            this.melodie=melodie;
            this.artist=artist;
            this.an=an;
        }

        public String toString()
        {
            return "Melodie: "+melodie+" Artist: "+artist+" An aparitie: "+an;
        }
    }

    public static void main(String[] args) throws SQLException {
        String url="jdbc:mysql://localhost:3306/test";
        Connection con = DriverManager.getConnection(url, "root", "root");
        Statement sql;
        sql=(Statement) con.createStatement();
        ResultSet rs;
        rs=sql.executeQuery("select * from melodie");
        JFrame f=new JFrame("Melodii");
        f.setSize(300, 300);
        f.setLayout(new BorderLayout());
        JTextArea t=new JTextArea();
        JButton b=new JButton("Stergere");
        b.setSize(30, 20);

        while(rs.next())
        {

            System.out.println(rs.getString("melodie")+rs.getString("artist")+rs.getInt("an"));
            Melodii m=new Melodii(rs.getString("melodie"), rs.getString("artist"), rs.getInt("an"));
            lista.add(m);
            for(int i=0; i<lista.size();i++)
            {
                t.setText(m.toString());
            }
        }


        f.add(t, BorderLayout.CENTER);
        f.add(b, BorderLayout.SOUTH);
        f.setVisible(true);

    }

}
deHaar :

A problem in your code is this part:

while(rs.next()) {
    System.out.println(rs.getString("melodie") + rs.getString("artist") + rs.getInt("an"));
    Melodii m = new Melodii(rs.getString("melodie"), rs.getString("artist"), rs.getInt("an"));
    lista.add(m);

    for (int i = 0; i < lista.size(); i++) {
        t.setText(m.toString());
    }
}

You are creating a Melodii and adding it to lista, but then you loop through lista and override the previously set text in it.

I recommend not to loop through the entire list while the ResultSet has not been completely iterated. Move the for loop below the while loop and apply append(m.toString()) instead of setText(m.toString()), like

while(rs.next()) {
    System.out.println(rs.getString("melodie") + rs.getString("artist") + rs.getInt("an"));
    Melodii m = new Melodii(rs.getString("melodie"), rs.getString("artist"), rs.getInt("an"));
    // just store all the results from the database, no need to iterate it at this point
    lista.add(m);
}
// instead, iterate the list afterwards and append the text
for (int i = 0; i < lista.size(); i++) {
    t.append(m.toString());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358572&siteId=1