Reputation: 929
I'm trying to select the names of the items from the database and then save it into an array to later add it to the JComboBox but for some reason it's not taking it from the database at all.
Here's the part of the code which is supposed to do that:
public BirthdayForm()
{
super("Birthday Party Supplies Rental Form");
String url = "jdbc:mysql://localhost/";
String dbName = "partySupplies";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String pw = "";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,username,pw);
setLayout(new FlowLayout());
String[] list = null;
chooseItem = new JLabel("Choose Item:");
String selectSQL = "SELECT productName FROM birthday where productName = ?";
PreparedStatement preparedStatement = conn.prepareStatement(selectSQL);
ResultSet rs = preparedStatement.executeQuery(selectSQL );
while (rs.next()) {
String name = rs.getString(2);
System.out.println(name);
for(int i=0; i<8; i++){
list[i] = name;
}
}
items = new JComboBox(list);
chooseQuantity = new JLabel("Choose Quantity:");
quantity = new JTextField("1");
choose = new JPanel();
choose.setLayout(new GridLayout(1,4));
choose.add(chooseItem);
choose.add(items);
choose.add(chooseQuantity);
choose.add(quantity);
add(choose);
//other codes
Upvotes: 1
Views: 207
Reputation: 1315
You have to select it like this from your database.
combobox.addItem(rs.getString("items"));
It takes the "items" from your resultset and it puts it into a String and it adds it to your combobox.
Upvotes: 1
Reputation: 1315
You can add Strings to a JComboBox like this:
String item1 = "This is my first item";
String item2 = "This is my second item";
combo.addItem(item1);
combo.addItem(item2);
In the end it looks like this:
Upvotes: 0