如何在 JList Swing 中添加数据库的检索数据?

How to add retrieved data of database in JList Swing?

我已经完成了电影票预订的代码,现在我已经停在 JList

如何在JList中添加数据?

这是一个代码...

    JList listTime = new JList();
    listTime.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listTime.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    listTime.setBounds(329, 311, 200, 50);
    frame.getContentPane().add(listTime);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suhel","root","Suhel786");
        Statement stmt = con.createStatement();
        String sql="select distinct time from tbmovie where movie='"+select_mov+"'";
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next())
        {
            listTime.add(rs.getString("time"));//here is the Error
        }

    }
    catch(Exception e1){
        System.out.print(e1);
    }

我已从数据库中检索数据 - 只需要在列表中添加数据。

首先,使用 ListModel 初始化 JList,例如a DefaultListModel 然后用它的方法修改 ListModel。或者,将项目从数据库加载到数组中,然后如果可能的话使用接受数组的构造函数创建 JList。