Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java将值从数据库加载到组合框中_Java - Fatal编程技术网

java将值从数据库加载到组合框中

java将值从数据库加载到组合框中,java,Java,嗨,我的目标是从数据库中加载带有vaules的combobox下面的代码可以很好地解决一个问题我得到了第一项中的两个,所以我必须做些什么来防止这种情况 public void loadCombos() { try { try { String cs = "jdbc:mysql://localhost:3307/booksalvation6"; String user = "root"; String

嗨,我的目标是从数据库中加载带有vaules的combobox下面的代码可以很好地解决一个问题我得到了第一项中的两个,所以我必须做些什么来防止这种情况

public void loadCombos() {
    try {
        try {
            String cs = "jdbc:mysql://localhost:3307/booksalvation6";
            String user = "root";
            String password = "letmein";
            jComboBox2.removeAllItems();// make sure old data gone
            PreparedStatement pstpost;
            ResultSet rspost;
            conCombos = DriverManager.getConnection(cs, user, password);

            for (int i = 1; i < 11; i++) {

                String querypost = "select * from post "
                        + "WHERE postage_id =" + i;

                // load postage selections
                pstpost = conCombos.prepareStatement(querypost);
                rspost = pstpost.executeQuery();

                while (rspost.next()) {
                    String Mypost = rspost.getString(6);
                    jComboBox2.addItem(Mypost);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(BasicFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        conCombos.close();
    } catch (SQLException ex) {
        Logger.getLogger(BasicFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public void loadCombos(){
试一试{
试一试{
String cs=“jdbc:mysql://localhost:3307/booksalvation6";
字符串user=“root”;
字符串password=“letmein”;
jcombox2.removeAllItems();//确保旧数据已丢失
编制报表;
结果集rspost;
conCombos=DriverManager.getConnection(cs、用户、密码);
对于(int i=1;i<11;i++){
String querypost=“从帖子中选择*
+“WHERE postage_id=“+i;
//加载邮资选择
pstpost=conCombos.preparest陈述(querypost);
rspost=pstpost.executeQuery();
while(rspost.next()){
字符串Mypost=rspost.getString(6);
JCombox2.附加项(Mypost);
}
}
}catch(SQLException-ex){
Logger.getLogger(BasicFrame.class.getName()).log(Level.SEVERE,null,ex);
}
conCombos.close();
}catch(SQLException-ex){
Logger.getLogger(BasicFrame.class.getName()).log(Level.SEVERE,null,ex);
}
}

您正在使用
PreparedStatement
,但使用方式不正确

因为您只查找一列来获取邮资id在1到10之间的所有列值

您可以在单个查询中实现:

select unique(combo_value_column_name) from post 
                                       where postage_id>=? and postage_id<=?
从post中选择唯一(组合值列名称)
哪里有邮资_id>=?邮资及身份证