hk96 hk96 - 2 years ago 161
Java Question

JComboBox only show one item from database

I'm trying to display database item into a

and this is my code.

public static void checkItemName(){
Connection conn = SQLite.SQLite();
String sql = "select itemname from item";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while ({
String list = resultSet.getString("itemname");
} catch (SQLException lol){


I did declare
static JComboBox purcItemName;
purcItemName = new JComboBox();

The method/function will be called then user press login button.

The problem I'm having now is that, it only shows one item while my database has multiple items.

Anyone got an idea why?

Answer Source
        Vector v = new Vector();
        while ({
            String list = resultSet.getString("itemname");

        purcItemName.setModel(new DefaultComboBoxModel(v));

store the data you got from database in a vector object and once that is completed set the vector object in your combobox as a new model. try this one and don't close the connection inside your loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download