aspose file tools*
The moose likes JDBC and the fly likes populating combobox from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "populating combobox from database" Watch "populating combobox from database" New topic
Author

populating combobox from database

kessy cruz
Greenhorn

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:
try{
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);
while(rs.next()){
// items[index]= rs.getString("CodeId");
// jComboBox.addItem(rs.getString("CodeId"));

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

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

for (int i = 1; i < course; i++){
jComboBox.insertItemAt(getCodeId[i], i);
}
}
rset.close();
stst.close();
}catch (SQLException e){
e.printStackTrace();
}
} </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
Greenhorn

Joined: Apr 03, 2013
Posts: 29
here instead of using
model.addElement() you should use JcomboBox.addItem();
thankyou..
 
 
subject: populating combobox from database