cbox.addItem(strings);
is adding the List
as single element to the JComboBox
.
As you have previously been advised, you should add the values to a ComboBoxModel
, for example...
DefaultComboBoxModel model = new DefaultComboBoxModel();
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()){
model.addElement(rs.getString("Name"));
}
} catch (SQLException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
cbox.setModel(model);
Take a look at How to Use Combo Boxes for more details
You may also want to take a look at Using Prepared Statements as you are not using them properly...which was also highlighted in your previous question...
As you have previously been advised, you should be closing your resources to the database when you are done with them, failing to do so could impare you databases performance...
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
//...
} catch (SQLException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
rs.close();
} catch (Exception exp) {
}
try {
stmt.close();
} catch (Exception exp) {
}
try {
con.close();
} catch (Exception exp) {
}
}
Or if you're using Java 7
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772")){
try (PreparedStatement stmt = con.prepareStatement("SELECT Name FROM Discount WHERE Type = \"" + type + "\"")) {
try (ResultSet rs = stmt.executeQuery) {
//...
} catch (SQLException exp) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SQLException exp) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SQLException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}