我的组合框正在复制字符串

时间:2015-09-27 00:45:19

标签: combobox

帮助,combobox只是继续添加项目,我尝试使用removeallitems,但之后我不能在第一个组合框上放任何东西

public class Function {

    public void combofillsect(JComboBox section, String year){
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    String query;

    try{

        query = "Select Section from asd where Year=?";
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); 
        pst = conn.prepareStatement(query);
        pst.setString(1, year);
        rs = pst.executeQuery();

        while(rs.next()){
        section.addItem(rs.getString("Section"));

        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
        section.addItem(e.toString());

    };        
}




Function funct= new Function();      
         {funct.combofillsect(jComboBox1,String.valueOf(jComboBox2.getSelectedItem()));    }

为什么我不能张贴图片?

1 个答案:

答案 0 :(得分:0)

你是用C#编程的吗?如果是这种情况,那么您可以使用清除功能:yourComboBox.Items.Clear()删除所有当前项目。我不知道它是否能解决您的问题,但是您从数据库获取数据的技术对我来说似乎很奇怪,如果您使用了数据集,您可以dataset.Tables(0).Rows.Count()来获取条目数,然后像这样设置循环的退出条件 - &gt; counter < dataset.Tables(0).Rows.Count(),并在你的结束时设置一个反问题++(也许这就是为什么你说你的组合框不会停止填充,但我不知道{{1}功能)。

我不知道C#代码,但有我的VB.NET功能:

next()

我不知道我是否帮助了你,但我真的不明白你的问题是什么,你甚至没有提到你使用的语言^^祝你好运

编辑:如果您无法发布图片,因为您还没有10点声誉,您可以在此处获取有关声誉的信息:https://stackoverflow.com/help/whats-reputation,但您仍然可以发布图片链接,以便用户点击它