Click here to Skip to main content
15,885,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There are two ComboBoxes "jComboCins1 and jComboCinsM". I want to populate jComboCinsM depending on jComboCins1 from postgresql. Here is the code, no errors but still cannot populate. Thanks in advance for all replies.

Java


public class MekleFrame extends javax.swing.JFrame {


        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pst = null;
        Statement st = null;

    /**
     * Creates new form MekleFrame
     */
    public MekleFrame() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        jTextFielddummy.setVisible(false);



       conn = dbConnection.ConnectDB();
       update_table();
       pop_combo();
    }
private void pop_combo(){

      try {    
        //String cnsCo=jComboCins1.getSelectedItem().toString();
        String sql = "SELECT * from cinsdb WHERE sinif= ?";
        pst=conn.prepareStatement(sql);
        //pst.setString(1,cnsCo);
        pst.setString(1,String.valueOf(jComboCins1.getSelectedItem()));
        rs=pst.executeQuery();

        while (rs.next())
        {
        String cinsad = rs.getString("cinsad");
        jComboCinsM.addItem(cinsad);
        }
    }
    catch(Exception e){

    JOptionPane.showMessageDialog(null, e);

    }

    }
}


What I have tried:

If i change the SQL Query without "where" and disable pst.setString() i can populate ComboBox. I guess there is someting wrong with the ResultSet or PreparedStatement. I tried the SQL Query as "SELECT cinsad from cinsdb WHERE sinif= ?", didnt work either. By the way SQL connection and Queries work fine, tested.
Posted
Comments
[no name] 24-Jun-20 8:23am    
You sure that `jComboCins1` has some value? becasue if it doesn't, so `jComboCins1.getSelectedItem()` will return `null`
Also you probably should remove previous added items to `jComboCinsM` before adding new ones.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900