Hello everyone

I m using java and ms access.

I want to retrieve values from database into combo box.
When I m trying to do this, only first value is getting displayed in combo box insted of list of all items.

can you plz help me with the code?

Thanx in advance...

    try
    {
        stmt = conn.createStatement();

        rs = stmt.executeQuery("select a1 from TableA");
        while(rs.next())
        {
            String s = rs.getString(1);
           System.out.println(s);              
        }
    }

This code is printing all the items in database.
but when I m writing

              c1.addItem(rs.getString(1));

its displaying only one item in combo box.

Can u plz tell me what is going wrong here?

Thanx in advance.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

First, statement c1.addItem(rs.getString(1)); must be placed inside the loop.

while(rs.next())
{
c1.addItem(rs.getString(1));
}

Use code tags. Source code must be surrounded with code tags.

Edited 3 Years Ago by happygeek: fixed formatting

    try
    {
        stmt = conn.createStatement();

        rs = stmt.executeQuery("select a1 from TableA");
        while(rs.next())
        {
            c1.addItem(rs.getString(1));
        }
    }

this also displaying only one item

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Then the query is either returning only one item, or you have done something wrong with "c1". But this code won't show us that. In any case, you shouldn't be mixing your GUI code and your DB code in this way. Simply read the resultset adding the values to a Vector then, preferably in another method and another class, set all options in the combobox at once by simply replacing it's list with the Vector by doing

c1.setModel(new DefaultComboBoxModel(vector));

Hi
Where should I write the code so that the values are displayed in the combo box once the program is run?

This article has been dead for over six months. Start a new discussion instead.