代码之家  ›  专栏  ›  技术社区  ›  Suhel shaikh

如何在jlist swing中添加数据库的检索数据?

  •  0
  • Suhel shaikh  · 技术社区  · 6 年前

    我已经做了电影票预订的代码,现在我在 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);
        }
    

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   am9417    6 年前

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