如何将mysql中的数据插入组合框中?

时间:2021-08-22 11:29:46

What's wrong with my code here?

这里的代码出了什么问题?

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

我正在尝试将mysql中的数据插入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");
    }
}

2 个解决方案

#1


0  

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

有时您会尝试以这种方式使用Model或使用Vector。最好尝试做类似的事情,

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

#2


0  

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

也许你的groupNames.toArray()方法不适合DefaultComboBoxModel()construktor。

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

您可以尝试将项目逐个放入ArrayList中:

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

Thats how I fill my comboboxes.

多数民众赞成我如何填补我的组合框。

#1


0  

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

有时您会尝试以这种方式使用Model或使用Vector。最好尝试做类似的事情,

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

#2


0  

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

也许你的groupNames.toArray()方法不适合DefaultComboBoxModel()construktor。

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

您可以尝试将项目逐个放入ArrayList中:

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

Thats how I fill my comboboxes.

多数民众赞成我如何填补我的组合框。