Hi,
I am new to java.Can u Plz help me.I have a JcomboBox. I want to display all the ItemId from the database in that JComboBox. I don't know how to do that.Its Displaying only 1 item.ie.first record.Can any one plz help me.

The code is:-

public String  selectItemID(){
    String itemNo = null;

    try {
        statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
         ResultSet itemNumberResult = statement.executeQuery();
   while(itemNumberResult.next()) 
         {
             itemNo = itemNumberResult.getString(1);
             System.out.println("itemNo  " +itemNo);
              //itemNumber = ArrayList.class. 
             //System.out.println(" itemNumber = " + itemNumber);
              return  itemNo ;
         }
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return itemNo;

}

DbActivity dbconnection = new DbActivity();

 String  result= dbconnection.selectItemID();
  itemno.addItem(result);

The while loop only runs one time because of the statement

return itemNo ;

Once that statement executes the loop (and the entire method) are ended.

Take out the return and it should work. Why are you putting a return there anyways?

:cool: For more help, www.NeedProgrammingHelp.com

Hi ,
I commented the return statement. It is printing all the itemId in the Console.but in the combobox it is printing only the last record id. Can u please tell me what to do.

The code is:-

public String selectItemID(){
String itemNo = null;

try {
statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
ResultSet itemNumberResult = statement.executeQuery();
while(itemNumberResult.next())
{
itemNo = itemNumberResult.getString(1);
System.out.println("itemNo " +itemNo);
//return itemNo ;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return itemNo;

}
itemno=new JComboBox();
DbActivity dbconnection = new DbActivity();

String result= dbconnection.selectItemID();
itemno.addItem(result);
System.out.println("Result "+result);

It does exactly what you tell it to do.
First you loop over the entire resultset, then you add the current entry (which at that point is the last one) to the control.

I suggest you analyse what you're doing, you'll soon enough figure out why it's not what you want and how to fix it.
That way you'll actually learn from your mistakes, as opposed to reading a ready-made solution.

Hi,
I used Vector.but the records are not displaying in the jcombobox.It is printing the values in the console. I think the problem is with the line 'itemno=new JComboBox(result);' . I am getting the values from the selectItemID() method. but it is not assigning it to the jcombobox.

The Code is:-

public Vector selectItemID(){
Vector result = new Vector();

try {
statement = dbConnection.prepareStatement("select ItemId from ItemDetails");
ResultSet itemNumberResult = statement.executeQuery();
while(itemNumberResult.next())
{
String itemNo = itemNumberResult.getString(1);
System.out.println("itemNo " +itemNo);
result.add(itemNo); // add all found itemNos
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;

}


DbActivity dbconnection = new DbActivity();

Vector result= dbconnection.selectItemID();
itemno=new JComboBox(result); // construct Combo with Vector
System.out.println("Result "+result);

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.