1

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

    }

}
3
  • because your Melodii class is static Commented Dec 17, 2019 at 12:46
  • 3
    You are not appending the text, you are setting each result read, which will be fast so you just see the last one. The method t.setText(m.toString()) overrides previously set text. Try t.append(m.toString());, you may have to add some line breaks. Commented Dec 17, 2019 at 12:47
  • @CristinaMoroti Great... I have made an answer out of my comment. Commented Dec 17, 2019 at 12:57

1 Answer 1

2

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.