Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArrayList不会在Java中存储数据库中的所有元素_Java_Mysql - Fatal编程技术网

ArrayList不会在Java中存储数据库中的所有元素

ArrayList不会在Java中存储数据库中的所有元素,java,mysql,Java,Mysql,我有以下代码。但是,当我运行它时,我在JTextArea中看不到数据库中的所有元素,只看到最后一个。但是如果我在while循环中打印出从数据库读取的内容,我会得到所有内容,所以我想问题在于对象列表。有什么建议可以解决这个问题吗 public class Muzica { static ArrayList <Melodii> lista=new ArrayList<Melodii>(); static class Melodii {

我有以下代码。但是,当我运行它时,我在JTextArea中看不到数据库中的所有元素,只看到最后一个。但是如果我在while循环中打印出从数据库读取的内容,我会得到所有内容,所以我想问题在于对象列表。有什么建议可以解决这个问题吗

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

    }

}

代码中的一个问题是这一部分:

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

因为你的Melodii类是静态的,你没有附加文本,你正在设置每个读取的结果,这将很快,所以你只看到最后一个。方法t.setTextm.toString覆盖以前设置的文本。试试t.appendm.toString;,您可能需要添加一些换行符。@CristinaMoroti太好了。。。我已根据我的评论作出了答复。
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());
}