Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

populating combobox from database

 
kessy cruz
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here instead of using
model.addElement() you should use JcomboBox.addItem();
thankyou..
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fatih Keles wrote: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.



I am getting an error on using Netbeans:

 
Dave Tolls
Ranch Hand
Posts: 1533
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That error mentions GetConnection.
The code shows a getConnection() method.

Java is case sensitive.
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That error mentions GetConnection.
The code shows a getConnection() method.

Java is case sensitive.


I did change the name, but this is not the reason why I am getting the error. It would have mentioned that GetConnection did not exist.

Here is my method for the GetConnection method.



Below is the call to it.
 
Dave Tolls
Ranch Hand
Posts: 1533
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Smit wrote:
Dave Tolls wrote:That error mentions GetConnection.
The code shows a getConnection() method.

Java is case sensitive.


I did change the name, but this is not the reason why I am getting the error. It would have mentioned that GetConnection did not exist.


It does:
cannot find symbol
symbol: method GetConnection()


Here is my method for the GetConnection method.



Below is the call to it.


So is it compiling, because that bit I posted above says it wasn't compiling?
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to create a class for this code.

Now I want to create an instance of the class, in another class which I have.

But I am struggling to set my ComboBox to the data.

e.g.



---
What is the best way of calling the method to populate the data into my ComboBox which is in another class file?
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have managed to get this to work on my application.
It was more of changing some of the code to suit the application.
It is so far working. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic