Filling a combobox with MySql data

Reply

Join Date: Jan 2008
Posts: 4
Reputation: GeKKeR is an unknown quantity at this point 
Solved Threads: 0
GeKKeR GeKKeR is offline Offline
Newbie Poster

Filling a combobox with MySql data

 
0
  #1
Jan 18th, 2008
Good Afternoon all,

I'm working on a project for school, and we have a part where we want a combobox filled with data from a mysql database, tabel = sport.

We can work out how to fill a normal combobox just with strings, but we can't find out how to link it to our mysqldatabase.

If anyone could help we would be very pleased.

btw, we can make the connection to our database and put data in, and we're almost there with getting it on our screen.

Thanks in advance, GeKKeR as member of the projectgroup I-6
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Filling a combobox with MySql data

 
0
  #2
Jan 18th, 2008
JComboBox is very flexible in that you can put any object you like in the model. You don't mention what you mean exactly by "link it to the database", but if you need to do something like get an ID for a selected entry, a small TypeEntry object can be used
  1. class TypeEntry{
  2. private int value;
  3. private String label;
  4.  
  5. public TypeEntry(int id, String label){
  6. this.value = id;
  7. this.label = label;
  8. }
  9. public int getValue(){
  10. return value;
  11. }
  12. public String toString(){
  13. return label;
  14. }
  15. }
and added to the combo box model like so
  1. rs = stmt.executeQuery("SELECT Id,Label FROM SomeTable ORDER BY Id");
  2. while (rs.next()){
  3. comboBox.addItem(new TypeEntry(rs.getInt(1),rs.getString(2).trim()));
  4. }
  5. rs.close();
The toString() value will be shown in the combo box and any other data you need from the selected entry can be obtained from the object returned by getSelectedItem().
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Filling a combobox with MySql data

 
0
  #3
Jan 18th, 2008
Standard Swing components have no database coupling, which is as it should be (you should not mix database code with user interface code).

Get the data from the database, and use that data to fill your user interface.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC