简体   繁体   English

将资料从MySQL载入JComboBox

[英]Load data from MySQL into JComboBox

I have two MySQL tables as image below 我有两个MySQL表,如下图

movie 电影 在此处输入图片说明

movie_title 电影标题

在此处输入图片说明

         JComboBox combo = new JComboBox();
        combo.setBounds(125, 15, 190, 20);
         try {   
            DatabaseConnection db=new DatabaseConnection();
            Connection connect=db.getConnection();
            String sql="Select title FROM movie_title";
            PreparedStatement ps=connect.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
            String name = rs.getString("title");
            combo.addItem(name);            
        }

    } catch (Exception e) {
        System.out.println("null");
    }
         combo.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent event)
    {
        JComboBox comboBox=(JComboBox) event.getSource();
         Object selected = comboBox.getSelectedItem();
         displayDay(selected);
    }

    private void displayDay(Object selected) {
        // TODO Auto-generated method stub
         try {  
                combo1.removeAllItems();
                DatabaseConnection db=new DatabaseConnection();
                Connection connect=db.getConnection();
                String sql="Select movie_day FROM movie WHERE movie_title = ?";
                PreparedStatement ps=connect.prepareStatement(sql);
                ps.setObject(1, selected);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                String day = rs.getString("movie_day");
                combo1.addItem(day);   
            }

        } catch (Exception e) {
            System.out.println("null");
        }

    }
        });

I have implemented ActionListener in comboBox . 我已经在comboBox实现了ActionListener When user select movie Marvel's Captain America, it will get the movie_day item from movie and load into combo1. 当用户选择电影“漫威美国队长”时,它将从电影中获取movie_day项目并加载到combo1中。 Is there a way I can make the combo1 display the movie_day which is Sunday, 28 Apr 2016 one time only instead of two ? 有什么办法可以使combo1只显示2016年4月28日(星期日)的movie_day而不是两次?

Edit 编辑

private void displayDay(Object selected) {
        // TODO Auto-generated method stub
         try {  
                combo1.removeAllItems();
                DatabaseConnection db=new DatabaseConnection();
                Connection connect=db.getConnection();
                String sql="Select movie_day FROM movie WHERE movie_title = ?";
                PreparedStatement ps=connect.prepareStatement(sql);
                ps.setObject(1, selected);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                String day = rs.getString("movie_day");
                DefaultComboBoxModel model = (DefaultComboBoxModel)combo1.getModel();                
                if (model.getIndexOf(day) == -1)
                {
                    combo1.addItem(day);
                }

            }

        } catch (Exception e) {
            System.out.println("null");
        }

Is there a way I can make the combo1 display the movie_day which is Sunday, 28 Apr 2016 one time only instead of two ? 有什么办法可以使combo1只显示2016年4月28日(星期日)的movie_day而不是两次?

Before adding the date to the combo box you need to check if the date already exists. 在将日期添加到组合框之前,您需要检查日期是否已经存在。

DefaultComboBoxModel model = (DefaultComboBoxModel)comboBox.getModel();

if (model.getIndexOf(theDate) == -1)
{
    comboBox.addItem( theDate );
}

You could also change the SQL statement to only get "unique" dates, but I don't know SQL well enough to give you the actual syntax. 您也可以将SQL语句更改为仅获取“唯一”日期,但是我对SQL的了解不够深,无法为您提供实际的语法。 Maybe this SQL Tutorial will help, otherwise you need to find a better tutorial. 也许此SQL教程会有所帮助,否则您需要找到一个更好的教程。

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

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