Java & Netbeans: Load data from MySQL to jCombobox

The code below is use for loading data from MySQL database to jCombobox. The difference between loading to jTable and jCombobox, the jCombobox load data on selected column from MySQL table. The jCombox can only load 1 column from MySQL table dynamically.

1.Create a new Method inside your class and insert the code below.

try {
    String sql="SELECT * FROM tablename";
    pst=conn.prepareStatement(sql);
    rs = pst.executeQuery();
    while(rs.next()){
        jComboboxName.addItem(rs.getString("columnname"));
    }
}catch(Exception e) {
    JOptionPane.showMessageDialog(this, e.getMessage());
}

2.Copy the method name and insert it inside the public method after “initComponents();”.

3.Done

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz