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...

Recommended Answers

All 6 Replies

Seeing your code would help.

    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.

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.

    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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.