5

What's wrong with my code here?

I'm trying to insert data from the mysql into the combobox in netbean

private void btnSandoghMousePressed(java.awt.event.MouseEvent evt) {                                        
    try {
        String query = "SELECT `AccountType` FROM `account`"; 
        con = Connect.ConnectDB();
        PreparedStatement stm = con.prepareStatement(query); 
        pst = con.prepareStatement(query);                
        ResultSet rs = pst.executeQuery(query);
        ArrayList<String> groupNames = new ArrayList<String>(); 
        while (rs.next()) { 
            String groupName = rs.getString(4); 
            groupNames.add(groupName);
        } 
        DefaultComboBoxModel model = new DefaultComboBoxModel(groupNames.toArray());
        cmbSemetarID.setModel(model);
        rs.close();    
    } catch (SQLException e) {
    System.err.println("Connection Error! it's about date");
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Is the model populated properly? – Smutje Jun 26 '14 at 06:33
  • Yes the model is fine,ArrayList groupNames = new ArrayList(); – Hekmat Sarwarzade Jun 26 '14 at 06:37
  • 1
    did you tried this? `DefaultComboBoxModel model = new DefaultComboBoxModel(); for(String groupname : groupNames) { model.addElement(groupname); } ` You can put your results one by one into the comboboxmodel. Maybe it is better instead to init the DefaultComboBoxModel with the `.toArray()` method of your groupNames. – Rubinum Jun 26 '14 at 06:40
  • did you get any error? Is there data in the araylist? – AJJ Jun 26 '14 at 06:53
  • 3
    (really do not understand three upvotes to basics question, contains bunch of mistakes, daily asked here, voting to close) do not to create any Java Object (used longer in app) in try - catch - finally block, create DefaultComboBoxModel as local variable, inside loop to add only a new Items, JDBC should be close() in finally otherwise stays in memory until current JVM exist – mKorbel Jun 26 '14 at 06:56
  • dont critic his way of proceeding to get his goal. Maybe he is new at java and need some improvements. everyone start at zero. :) – Rubinum Jun 26 '14 at 07:06
  • 1
    `btnSandoghMousePressed` implies a button. Don't use a `MouseListener` for buttons. Use `ActionListener` instead – Paul Samsotha Jun 26 '14 at 07:15
  • @Rubinum agree this is critics, disagree with question asked here, is so easy to ask whats wrong with my code, instead of to read standard Oracles tutorials about JDBC and JComboBox, – mKorbel Jun 26 '14 at 07:17
  • @mKorbel thats true. ^^ – Rubinum Jun 26 '14 at 07:46

2 Answers2

0

You get problems sometimes you try to use Model this way or using a Vector. Better try to do something like,

  private void btnSandoghMousePressed(java.awt.event.MouseEvent evt){                                        
    try {
        String query = "SELECT `AccountType` FROM `account`"; 
        con = Connect.ConnectDB();
        PreparedStatement stm = con.prepareStatement(query); 
        pst = con.prepareStatement(query);                
        ResultSet rs = pst.executeQuery(query);
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        while (rs.next()) { 
            String groupName = rs.getString(4); 
            model.add(groupName);
        } 

        cmbSemetarID.setModel(model);
        rs.close();    
    } catch (SQLException e) {
    System.err.println("Connection Error! it's about date");
    }
}
gprathour
  • 14,813
  • 5
  • 66
  • 90
0

Maybe your groupNames.toArray() method does not "fit" into the DefaultComboBoxModel() construktor.

You can try to put your items into your ArrayList one by one with this:

DefaultComboBoxModel model = new DefaultComboBoxModel();
for(String groupname : groupNames) 
{
  model.addElement(groupname);
}
cmbSemetarID.setModel();

Thats how I fill my comboboxes.

Rubinum
  • 547
  • 3
  • 18