简体   繁体   English

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

[英]How to add retrieved data of database in JList Swing?

I have done the code for Movie Ticket Booking and now I have stopped at JList . 我已经完成了电影票预订的代码,现在我已经在JList停止了。

How to add data in JList ? 如何在JList添加数据?

Here is a code... 这是一个代码...

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

I have retrieved data from database - only need is to add data in list. 我已从数据库中检索数据-只需在列表中添加数据。

First, initialize the JList with a ListModel, eg a DefaultListModel and then modify the ListModel with its methods. 首先,使用ListModel(例如DefaultListModel)初始化JList,然后使用其方法修改ListModel。 Alternatively, load the items from a database into an array, and create a JList afterwards with a constructor that accepts an array, if possible. 或者,将数据库中的项目加载到数组中,然后在可能的情况下使用接受数组的构造函数创建一个JList。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM