如何从一个列中选择不同的类别数据?

时间:2015-09-09 01:01:37

标签: java mysql swing jdbc

我的数据库中有一个名为product的表,列name pricecategory,有6个项目与2个主要类别相关。让我们说:

+----------+-------+-----------+
|  name    | price |  category |
+----------+-------+-----------+
|  mango   |  $58  |  fruits   |
|  avocado |  $74  |  fruits   |
|  apple   |  $20  |  fruits   |
|  carrot  |  $35  | vegetable |
|  onions  |  $40  | vegetable |
+----------+-------+-----------+

我想制作2 JComboBox,首先选择一个类别,然后选择另一个JComboBox来加载所有相关名称(如果我选择fruit,则会显示其他JComboBox所有的水果项目。)

这是我的代码,它从数据库中选择所有name并将其加载到组合框中。

  ArrayList<String> list = new ArrayList<String>();

        try {
            DBconnector db =  new DBconnector();
            con=  db.connect();
            String qr = "SELECT name FROM product";
            PreparedStatement stm = con.prepareStatement(qr);
            ResultSet rs = stm.executeQuery(qr);

            while(rs.next()){
                String name = rs.getString("name");
                //add group list to the arraylist
                list.add(name);
            }
            rs.close();

            //Populate the comboBox
            if(!list.isEmpty()){
            DefaultComboBoxModel model = new DefaultComboBoxModel(list.toArray());
            combItems.setModel(model);
            }else{
                System.out.println("List is empty...");
            }
        } catch (SQLException ex) {
            System.out.println("Data not retrieved");
            ex.printStackTrace();

        }
        return list;

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

这使用where子句选项和distinct,并显示如何将两个组合框与项侦听器连接:

            JFrame frm = new JFrame();
            JComboBox catBox = new JComboBox();
            final Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample","app","app");
            String getcatsQuery = "SELECT DISTINCT category from PRODUCT";
            PreparedStatement getcatsStatement = con.prepareStatement(getcatsQuery);
            ResultSet rs = getcatsStatement.executeQuery();
            List<String> list = new ArrayList<>();
            while (rs.next()) {
                String cat = rs.getString("category");
                //add group list to the arraylist
                list.add(cat);
            }
            rs.close();
            System.out.println("list = " + list);
            DefaultComboBoxModel catsModel = new DefaultComboBoxModel(list.toArray());
            catBox.setModel(catsModel);
            final JComboBox prodBox = new JComboBox();
            prodBox.addItem("             ");
            catBox.addItemListener(e -> {
                try {
                    String getprodsQuery = "SELECT name from PRODUCT where category=?";
                    PreparedStatement getprodsStatement = con.prepareStatement(getprodsQuery);
                    getprodsStatement.setString(1, catBox.getSelectedItem().toString());
                    ResultSet rs2 = getprodsStatement.executeQuery();
                    List<String> list2 = new ArrayList<>();
                    while (rs2.next()) {
                        String name = rs2.getString("name");
                        //add group list to the arraylist
                        list2.add(name);
                    }
                    rs.close();
                    DefaultComboBoxModel prodsModel = new DefaultComboBoxModel(list2.toArray());
                    prodBox.setModel(prodsModel);

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            });
            catBox.setSelectedIndex(0);
            frm.setLayout(new FlowLayout());
            frm.add(catBox);
            frm.add(prodBox);
            frm.pack();
            frm.setVisible(true);

答案 1 :(得分:0)

您有两个主要选项:在用户选择类别后,使用所选类别运行带有where子句的查询;或者创建一个从类别到名称列表的地图,然后用它来创建你的第二个组合框。

第二个选项看起来像:

Map<String, List<String>> categoryMap = new HashMap<>();
while(rs.next()){
    List<String> namesForCategory = categoryMap.getOrDefault(rs.getString("category"), new ArrayList<>());
    namesForCategory.add(rs.getString("name"));
    categoryMap.put(rs.getString("category"), namesForCategory);
}

现在,在选择类别后,您可以使用categoryMap.get(name)获取名称列表。

答案 2 :(得分:0)

您可能想要使用此query

String qr = "SELECT * FROM table_name WHERE category";

我认为你在那条线上遇到了麻烦。