0
votes

I used a combo box to retrieve data from MySQL database. But when i inserted the first value set to database, combo box values are repeating. I want to know why it is happening and how to avoid it. i called this method on main. Thanks

    public void FillCombo(){

    try{            
        String sql = "Select Name from Employee where Position='Driver'";
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();

        while(rs.next()){
            op.add(rs.getString("Name"));

        }
        selectDriverC.setItems(op);

        pst.close();
        rs.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}
1
What is op? Where is it defined? Are you sure it is empty when you call FillCombo, and are you sure there are no repetitive values in the database itself? - James_D
Op is a observerlist object. As i mean when i run it for 1st time values are fine. When i add 1st value set the values from database are repeating 3 times - nascar895

1 Answers

0
votes

Add a condition that checks if the String you wanna insert is already in op, I assume op type is ObservableList<String>. This should work :

public void FillCombo(){

    try{            
        String sql = "Select Name from Employee where Position='Driver'";
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();

        while(rs.next()){
            if(!op.contains(rs.getString("Name"))
            add(rs.getString("Name"));

        }
        selectDriverC.setItems(op);

        pst.close();
        rs.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}