This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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
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..
Jason Smit
Ranch Hand

Joined: Apr 19, 2012
Posts: 57
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

Joined: Sep 04, 2013
Posts: 721
    
    2
That error mentions GetConnection.
The code shows a getConnection() method.

Java is case sensitive.
Jason Smit
Ranch Hand

Joined: Apr 19, 2012
Posts: 57
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

Joined: Sep 04, 2013
Posts: 721
    
    2
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

Joined: Apr 19, 2012
Posts: 57
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

Joined: Apr 19, 2012
Posts: 57
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
 
jQuery in Action, 3rd edition
 
subject: populating combobox from database
 
It's not a secret anymore!