| | |
Filling a combobox with MySql data
![]() |
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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
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 and added to the combo box model like so 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().
java Syntax (Toggle Plain Text)
class TypeEntry{ private int value; private String label; public TypeEntry(int id, String label){ this.value = id; this.label = label; } public int getValue(){ return value; } public String toString(){ return label; } }
java Syntax (Toggle Plain Text)
rs = stmt.executeQuery("SELECT Id,Label FROM SomeTable ORDER BY Id"); while (rs.next()){ comboBox.addItem(new TypeEntry(rs.getInt(1),rs.getString(2).trim())); } rs.close();
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.
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.
![]() |
Similar Threads
- Filling a ComboBox.... (Java)
Other Threads in the Java Forum
- Previous Thread: Help me for main project....
- Next Thread: session creation failing in IE
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






