I want to add employee name from database to combobox
and i am using the coding

while(rs.next())
combobox.addItem(rs.getString("Emp_name"));

but the combobox appears consisting elements as Item1,Item2,Item3...and so on
how I can add employees names in combobox

Dani AI

Generated

For : seeing "Item 1, Item 2..." usually means the combo was left with design-time placeholders from the GUI builder or its model was never replaced. It can also happen if the ResultSet never actually returns rows because the query/statement was never executed. The quickest, robust approach is: build a fresh combo model from the ResultSet (so you don't depend on design-time items), make sure the query runs, and set the model on the EDT once it is ready.

A safe pattern that avoids UI freezes and resource leaks is: query the database with try-with-resources, populate a DefaultComboBoxModel<String>, then install that model on the JComboBox on the Event Dispatch Thread (or use a SwingWorker). That keeps DB code off the UI thread and replaces any placeholder items in one atomic step.

Example (adapt to your connection details and column name):

DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
String sql = "SELECT Emp_name FROM employees ORDER BY Emp_name";

try (Connection con = DriverManager.getConnection(url, user, pass);
     PreparedStatement ps = con.prepareStatement(sql);
     ResultSet rs = ps.executeQuery()) {

    while (rs.next()) {
        model.addElement(rs.getString("Emp_name"));
    }
}

SwingUtilities.invokeLater(() -> comboBox.setModel(model));

Notes and troubleshooting:

  • was right to suspect NetBeans placeholders; replacing the model handles that cleanly.
  • In 's sample the query execution and resource cleanup were missing; always call executeQuery and close resources (try-with-resources helps).
  • Do not call commit for read-only selects; handle SQLExceptions with e.printStackTrace() during testing.
  • Verify the column name exactly matches the DB (case and spelling) and that the combo variable is the one used by your UI form.

This approach makes the result repeatable and keeps the UI responsive.

Recommended Answers

All 2 Replies

hai Sweksha,

i think you are using netbeans IDE to develop the appilication.so that those values are coming by default with JCombobox.

for this , you have to call removeAllItems() avialable in the JCombobox class before inserting data into combobox

i mean calll the following statement before while loop

combobox.removeAllItems();
while(rs.next()){
// your remaining code goes here
}

thats it

let me know the status ......

happy coding

Try this:
(combobox1 is the variable name of the combobox you are using to display the emp_name

 public void fillCombo() {
        Connection con = null;
        ResultSet rs = null;
        java.sql.PreparedStatement st = null;
        Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "root");

        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", conProps);
            String sql = ("select * from TbName");


            while (rs.next()) {
                String y = rs.getString("emp_name");

                combobox1.addItem(y);

            }

            con.commit();
        } catch (Exception e) {

        e.printStack();
        }


    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.