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

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.