0

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

4
Contributors
6
Replies
13
Views
7 Years
Discussion Span
Last Post by rucha.cool4
0
    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 by mike_2000_17: Fixed formatting

0

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 by happygeek: fixed formatting

0
    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 by mike_2000_17: Fixed formatting

0

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));
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.