I have created a GUI and have a database located externally in which I fetch data from. I am using the GUI builder in NetBeans to do this. Does anyone know of a simple way to populate a jComboBox with values coming from the database? When I run the project there are no errors but the combo box remains empty.
Here is the code that sets the combo box with the names of discounts:
public void setDiscountNames(String type, JComboBox cbox) {
cbox.removeAllItems();
ArrayList<Discount> names = new ArrayList<Discount>();
try {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772");
stmt = con.prepareStatement("SELECT Name FROM Discount WHERE Type = \"" + type + "\"");
rs = stmt.executeQuery();
while(rs.next()){
cbox.addItem(rs.getString("Name"));
}
} catch (SQLException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
}
This is located in a seperate class from the jComboBox object. This class is called Model.
Here is the place I call the setDiscountNames method in a form called DiscountGUIView:
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt){
model.setDiscountNames("Fixed", jComboBox1);
}
Okay (Update) the query does print the results:
run:
Travel Standard Fixed Standard BUILD SUCCESSFUL (total time: 1 second)