如何在Java中将多个复选框值插入单个列数据库中

时间:2013-04-15 13:35:58

标签: java database checkbox

当然,如何在Java中将多个选中的复选框插入到数据库中。 我添加了“,”但它不起作用只存储了第一个选中的复选框。

我该如何解决这个问题?

这是我目前的代码:

String haspaper = null;

if(yes3.isSelected() == true){
    if(checkcontract.isSelected()==true){haspaper=checkcontract.getText()+",";}
    else if(checkcivile.isSelected()==true){haspaper=checkcivile.getText()+" , ";}
    else if(checkcontartpar.isSelected()==true){haspaper=checkcontartpar.getText()+" ,";}
    else {haspaper=mahiyapaper.getText()+" ,";}
}else{haspaper=no3.getText();}

3 个答案:

答案 0 :(得分:1)

如果条件为 haspaper 变量,则为每个值赋值。 根据您的逻辑附加值,而不是if if put if if block 喜欢:

haspaper += value

答案 1 :(得分:1)

您可以捕获servlet / jsp中的已检查值并将其作为对象存储在pojo / model类中,最后使用jdbc / hibernate存储到数据库...

答案 2 :(得分:1)

代码中的更正:

String haspaper="" ;
    if(yes3.isSelected()){
        if(checkcontract.isSelected()){
             haspaper = haspaper + checkcontract.getText()+",";
        }
        else if(checkcivile.isSelected()){
             haspaper = haspaper + checkcivile.getText()+" , ";
        }
        else if(checkcontartpar.isSelected()){
             haspaper = haspaper + checkcontartpar.getText()+" ,";
        }
        else {
             haspaper = haspaper + mahiyapaper.getText()+" ,";
        }
    }else{
         haspaper=no3.getText();
    }