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

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

Edited 3 Years Ago by radhakrishna.p: giving clarity on the solution

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();
        }


    }
This article has been dead for over six months. Start a new discussion instead.