Reputation: 107
I need to erase duplicates in Object example[]
And Object example is filled like this:
final Object example[] = new Object[rowCount];
try{
int row = 0;
Statement st = conn.createStatement();
rs = st.executeQuery("SELECT * FROM Table1");
while(rs.next()){
example[row] = rs.getString("Name");
row++;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "DBComboBoxFill error: " + e);
}
And i need it for: JComboBox combobox = new JComboBox(example)
I know how to do that whit Integers, 1st sort them, then check whit if statement and erase. I don't know, maybe i can do it whit ArrayList, but will the ComboBox get values from ArrayList?
Upvotes: 0
Views: 42
Reputation: 667
If the only column you want is Name (which is what it looks like from the code) then you can instead retrieve just that column in the query, and then you can use DISTINCT to avoid duplicates (as suggested by SubOptimal).
That is, change the query from SELECT * FROM Table1 to SELECT DISTINCT Name FROM Table1 as shown below.
final Object example[] = new Object[rowCount];
try{
int row = 0;
Statement st = conn.createStatement();
rs = st.executeQuery("SELECT DISTINCT Name FROM Table1");
while(rs.next()){
example[row] = rs.getString("Name");
row++;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "DBComboBoxFill error: " + e);
}
Upvotes: 1