How to fill a JComboBox in java?

0

Good day! I have a problem, I am trying to fill a JComboBox from mysql, and I can not! :( The console does not send me an error but it does not show anything either, I'm just starting in this and I do not have much experience! Hopefully someone can help me ... I leave the code here:

 // GridBagLayout
    this.setLayout(new GridBagLayout());
    GridBagConstraints left = new GridBagConstraints();
    GridBagConstraints right = new GridBagConstraints();
    GridBagConstraints wide = new GridBagConstraints();
    left.gridx = 0;
    left.anchor = GridBagConstraints.LINE_END;
    right.gridx = 1;
    right.anchor = GridBagConstraints.LINE_START;
    wide.gridwidth = 2;
    wide.fill = GridBagConstraints.HORIZONTAL;

    //*********Aqui esta mi combobox****
    wide.gridy++;
    patron = new JComboBox();
    this.add(patron, wide);       
    patron.setBackground(colorAzulM);

Here is my method, I'm doing it in the same class, I do not know if that has to do!

Pool metodospool = new Pool();
java.sql.Connection cn = null;
ResultSet rs = null;
Statement st=null;

private void cargarcombopatron() {
    try {
        String sql= "select Als from dig_hrpatron";
        cn= metodospool.dataSource.getConnection();
        cn.createStatement();
        rs = st.executeQuery(sql);
        System.out.println(sql);
        while(rs.next())
        {
            patron.addItem(rs.getString(1));
        }
    } catch (SQLException ex) { 
        Logger.getLogger(StructurePropertiesPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I hope your help !!! Thank you very much :)

    
asked by Letty Lopez 23.07.2018 в 17:33
source

1 answer

1

apparently you need to call the private void, then try to do this: ...

        String query = "select Als from dig_hrpatron";
        try {
            PreparedStatement pst = cn.prepareStatement(query);
            result = pst.executeQuery();
            while (result.next()) {
                patron.addItem(result.getString("Als")); // aca es donde cargas el combobox con el registro de la BD
            }

            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Loq_Editar.class.getName()).log(Level.SEVERE, null, ex);
        }
    
answered by 04.09.2018 в 21:45