aspose file tools
The moose likes JDBC and Relational Databases and the fly likes populating combobox from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "populating combobox from database" Watch "populating combobox from database" New topic

populating combobox from database

kessy cruz

Joined: Dec 13, 2010
Posts: 29

Good morning, I'm trying to populate comboBox from database. its my first time and i'm messing it all up, i cant find a good tutorial about it online.
please help structure the code.Thanks you for taking time to read this.

<code>private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
Statement stst = (Statement)conn.createStatement();
String sql = "SELECT CodeId FROM Courses";
int index = 0;
ResultSet rs = stst.executeQuery(sql);
// items[index]= rs.getString("CodeId");
// jComboBox.addItem(rs.getString("CodeId"));

//clears the contents of the combo box
Course course = new Course();

jComboBox.insertItemAt("Course", 0);
// jComboBox.addItem(rs.getString(""));

for (int i = 1; i < course; i++){
jComboBox.insertItemAt(getCodeId[i], i);
}catch (SQLException e){
} </code>
Fatih Keles
Ranch Hand

Joined: Sep 01, 2005
Posts: 182
Hi kessy,

Here is a sample code for you. What it does :
1) Creates a combobox
2) builds a model for combobox from items of type DemoModelItem (try commenting out overrided toString method)
3) sets combobox model

Make sure that you have visited "How to Use Combo Boxes" tutorial.

Neha Kaushik

Joined: Apr 03, 2013
Posts: 29
here instead of using
model.addElement() you should use JcomboBox.addItem();
I agree. Here's the link:
subject: populating combobox from database