java - 如何将数据从一个 JComboBox 传递到一条 SQL 语句,该 SQL 语句将调用第二个 JComboBox 的值?

标签 java database swing prepared-statement jcombobox

private void fillCombo(){
    try {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MTD","root","");
        PreparedStatement ps = con.prepareStatement("Select * from sa where municipalities ='"  + municipality.getSelectedItem().toString()+ "'");
        ResultSet rs = ps.executeQuery();

        while(rs.next()){
            String mun = rs.getString("Province");
            municipality.addItem(mun);
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }

我有两个组合框,可以从数据库中获取值。第一个组合(province)来自一个具有一列的表,该列是省份列表。因此,在 province.itemchangedstate 上,我希望它设置与 combo1< 选择的省份相关的 combo2 (市政)的值()。

我是java新手,一直坚持下去,不知道如何进一步进行。有人可以帮忙吗?

最佳答案

据我所知,您的要求是在选择其他组合框的基础上填充一个组合框。 喜欢 enter image description here

这个示例代码可能适合您。

private void populateDropDownOfEnum(){
        EncDecImageFrame encDecImageFrame = new EncDecImageFrame();

        jComboBox1.setEditable(true);
        String[] jComboBox1Values = getListOfAllEnumNames();
        if (jComboBox1Values != null && jComboBox1Values.length != 0) {
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(jComboBox1Values));
        }

        Object item = jComboBox1.getSelectedItem();
        jComboBox2.removeAllItems();
        jComboBox2.setEditable(true);
        String[] enumValuesByEnumNames = getListOfAllEnumValues(
                item.toString(), artifcatoryPathOfJar.getText());
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(
                enumValuesByEnumNames));

        jComboBox1.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                Object item = jComboBox1.getSelectedItem();
                jComboBox2.removeAllItems();
                jComboBox2.setEditable(true);
                String[] enumValuesByEnumNames = getListOfAllEnumValues(
                        item.toString(), artifcatoryPathOfJar.getText());
                jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(
                        enumValuesByEnumNames));

            }
        });

关于java - 如何将数据从一个 JComboBox 传递到一条 SQL 语句,该 SQL 语句将调用第二个 JComboBox 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30503363/

相关文章:

java - PersistenceException,列 'id' 指定两次

java - 如何使用 setLayout(null) 使 JPanel 保持在顶部(在 z 轴上)

Java、BlazeDS、Flex - 错误 #10566 : Cannot create property smallMessage on AcknowledgeMessage?

java - 绕过Java中的多重继承

MongoDB 单文档大小限制为 16MB

database - 在 Cloudant 中记录输出

java - 在Android中,什么线程可运行传递给Executors.newSingleThreadScheduledExecutor运行?

Java - 编程双向控制台窗口

java - 如何限制 JScrollPane 组件的宽度

java - 使用 Swing 将 Jpanels 正确添加到 BoxLayout 时遇到问题