基于连接到 PostgreSQL 数据库的两个 Jcomboboxes 填充 Jlist 时出现问题

Problem populating a Jlist based on two Jcomboboxes connected to a PostgreSQL database

下午好

我正在 中开展一个项目,我必须针对某些参数(例如旋转、级数、直径和粘度)计算泵的性能。起初,我使用带有多个商业泵的 PostgreSQL 创建了一个数据库。该数据库包含作为制造泵的公司的模式,在每个模式中有 tables 代表不同系列的泵,并且 tables 有几个泵组织成行。这些线包含代表泵的系数,是计算性能所必需的。

我尝试用 C++ 编写应用程序代码,但连接 Postgre 太难了,所以我结束了 Java 和 Netbeans 的工作(我认为这对新手来说更容易)。目前应用运行还不错,但我发现有两个问题无法解决。

要select 泵进行计算,我必须使用两个 Jcomboboxes 和一个 Jlist。第一个 Jcombobox 用于 select 制造商,第二个用于 select 系列。最后,Jlist 显示该系列中的所有泵,因此用户可以 select 一个。

我能够使用数据库的模式填充第一个 Jcombobox(实际上感谢你们),第二个使用模式的 tables 在第一个 selected和带有泵名称的 Jlist。

第一个问题是我在第一个Jcombobox中更改selected制造商后无法清除第二个Jcombobox(系列),每次更改第一个Jcombobox时它都会添加更多的itens,即使我重新 select 一个已经 select 的项目。我尝试使用命令 "removeAllItems()",但它只显示一个 table 并停止填充 Jcombobox。

第二个问题是,当我在第二个 Jcombobox 中 select 一个系列时,它不会立即在 Jlist 中显示泵,在我 select 另一个系列之后 Jlist 开始显示.所以,我必须 select 一个,然后换成另一个,这样泵就会开始出现在 Jlist 中。

对于冗长的文字和英文错误,我很抱歉,我希望这足以让你们理解。我知道代码不漂亮,那是因为我还不太擅长这个,我有点急于交付它。

这是代码,没有 Netbeans 生成的所有代码。

public final class LabPump extends javax.swing.JFrame {

Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;

public LabPump() {
    initComponents();

    con = ConnectPump.connect();

    FillSelectFabricante();

}

public void FillSelectFabricante(){

    try {
        rs = con.getMetaData().getSchemas();

        while(rs.next()){
            String schemas = rs.getString("TABLE_SCHEM");

            if(schemas.equals("information_schema") || schemas.equals("pg_catalog")){
            }
            else{
                selectFabricante.addItem(schemas);
            }
        }
    }
    catch(SQLException error){
        JOptionPane.showMessageDialog (null,error);
    }

}

public void FillSelectSerie(String fabricante){

    //selectSerie.removeAllItems();

    try {
        String[] sql = {"TABLE"};
        rs = con.getMetaData().getTables(null, fabricante, "%", sql);

        while(rs.next()){
            String tables = rs.getString(3);

            selectSerie.addItem(tables);
        }
    }
    catch(SQLException error){
        JOptionPane.showMessageDialog (null,error);
    }

}

public void FillListBomba(String fabricante, String serie){

    DefaultListModel dlm = new DefaultListModel();
    dlm.removeAllElements();

    try{
        String sql = "select * from " + fabricante + "." + serie;
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();

        while(rs.next()){
            String bomba = rs.getString("bomba");
            dlm.addElement(bomba);
        }
    listBomba.setModel(dlm);
    }
    catch(SQLException error){
        JOptionPane.showMessageDialog (null,error);
    } 

}

    private void selectSerieActionPerformed(java.awt.event.ActionEvent evt) {                                            

    selectSerie.addActionListener((ActionEvent e) -> {
        String fabricante = selectFabricante.getSelectedItem().toString();
        String serie = selectSerie.getSelectedItem().toString();

        FillListBomba(fabricante, serie);

    });
}                                           

private void selectFabricanteActionPerformed(java.awt.event.ActionEvent evt) {                                                 

    String fabricante = selectFabricante.getSelectedItem().toString();

    FillSelectSerie(fabricante);

}

谢谢大家

我刚刚成功解决了问题。当 selectSerie.removeAllItems() 行取消注释时,程序返回 NullPointerException 错误。

为了解决这个问题我使用了DefaultComboBoxModel class 然后使用了RemoveAllElements() 方法。我不太确定为什么其他方法不起作用。

感谢您的宝贵时间和帮助 Peter

public void FillSelectSerie(String fabricante) {

    DefaultComboBoxModel box;
    box = new DefaultComboBoxModel();
    box.removeAllElements();

    try {
        String[] sql = {"TABLE"};
        rs = con.getMetaData().getTables(null, fabricante, "%", sql);
        box.addElement("-");

        while (rs.next()) {
            String tables = rs.getString(3);
            box.addElement(tables);
        }
        selectSerie.setModel(box);
    } catch (SQLException error) {
        //JOptionPane.showMessageDialog (null,error);
    }
}