使复选框仅对应于sql中的5列

时间:2015-08-05 14:10:07

标签: java sql checkbox

我有9个复选框,我将选择限制为仅限5。然后将复选框插入到sql中。在我的sql中,我创建了一个菜单表,其中有8列名为Type,Budget,Day, Pre1,Pre2,Pre3,Pre4 and Pre5.(Pre1,Pre2,Pre3,Pre4,Pre5 used to store checkbox value)如果只检查了三个checke box,则另外两列将得到" 0"。怎么做?

User.java

int total=0; // limit the checkbox selection 
if(chckbxLei.isSelected())
{
    lei=Integer.valueOf(chckbxLei.getName());
    total++;
}
else
{
    chckbxLei.setName("0");
    lei=Integer.valueOf(chckbxLei.getName());   
}
if(chckbxAdv.isSelected())
{
    adv=Integer.valueOf(chckbxAdv.getName());
    total++;    
}
else
{
    chckbxAdv.setName("0");
    adv=Integer.valueOf(chckbxAdv.getName());   
}
if(chckbxHis.isSelected())
{
    his=Integer.valueOf(chckbxHis.getName());
    total++;
}
else{
chckbxHis.setName("0");
his=Integer.valueOf(chckbxHis.getName());   
}
if(chckbxOut.isSelected())
{
    out=Integer.valueOf(chckbxOut.getName());
    total++;
}
else{
    chckbxOut.setName("0");
    out=Integer.valueOf(chckbxOut.getName());   
}
if(chckbxFAK.isSelected())
{
    fak=Integer.valueOf(chckbxFAK.getName());
    total++;
}
else{
    chckbxFAK.setName("0");
    fak=Integer.valueOf(chckbxFAK.getName());   
}
if(chckbxSho.isSelected())
{
    sho=Integer.valueOf(chckbxSho.getName());
    total++;
}
else{
    chckbxSho.setName("0");
    sho=Integer.valueOf(chckbxSho.getName());   
}
if(chckbxMu.isSelected())
{
    mu=Integer.valueOf(chckbxMu.getName()); 
    total++;
}
else
{
    chckbxMu.setName("0");
    mu=Integer.valueOf(chckbxMu.getName()); 
}
if(chckbxEn.isSelected())
{
    en=Integer.valueOf(chckbxEn.getName()); 
    total++;
}

else{
    chckbxEn.setName("0");
    en=Integer.valueOf(chckbxEn.getName()); 
}
if(chckbxAr.isSelected())
{
    ar=Integer.valueOf(chckbxAr.getName()); 
    total++;
}

else{
    chckbxAr.setName("0");
    ar=Integer.valueOf(chckbxAr.getName()); 
}


if(total>5)
{
    JOptionPane.showMessageDialog(frame,
            "Please Select Only 5! ");
    return ;

}

Place place= new Place();
try {
    place.addMenu(a,b,day,lei,adv,his,out,fak,sho,mu,en,ar);// I want it only has 8 parameters! (a,b,day are fixed)
    JOptionPane.showMessageDialog(null,"Account Succesfully Created");

Place.java

public void addMenu(Integer aa, Integer bb, Integer cc,  Integer dd, Integer ee, Integer ff, Integer gg, Integer hh, Integer ii,Integer jj, Integer kk, Integer ll) throws Exception{
        DatabaseConnection db=new DatabaseConnection();
        Connection connect=db.getConnection();
        String sql="Insert into menu(Type,Budget,Day,Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9)VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps=connect.prepareStatement(sql);
        ps.setLong(1,aa);
        ps.setLong(2,bb);
        ps.setLong(3,cc);
        ps.setLong(4,dd);
        ps.setLong(5,ee);
        ps.setLong(6,ff);
        ps.setLong(7,gg);
        ps.setLong(8,hh);
        ps.setLong(9,ii);
        ps.setLong(10,jj);
        ps.setLong(11,kk);
        ps.setLong(12,ll);
        ps.executeUpdate();
        connect.close();
        ps.close();

    }

1 个答案:

答案 0 :(得分:1)

让我们从数据库设计开始。我通常会建议一个更规范化的模型,其中您可能有包含menu列的表(menu_id, type, day, budget)和另一个包含menu_preference(menu_id, preference)的表。如果您在某些时候需要同时允许六个或十个首选项,则允许您指定无限量的首选项。

您的问题可以表述为:列出每个所选复选框的值并将其存储到数据库中,如果列表少于5个,则用零填充列表。

要解决此问题,您实际上可以使用所选复选框的值填充ArrayList

List<Integer> preferences = new ArrayList<Integer>();
if(checkboxXXX.isSelected()) {
    preferences.add(Integer.valueof(checkboxXXX().getName());
}
...
if(preferences.size() > 5) {
   // alert
} else {
   // save
}

要进一步改进代码,您可以将阈值5分配给常量并提取appendValueifSelected(checkbox, preferences)方法。

不要编辑saveMenu方法。首先,优良作法是为参数使用描述性名称,因为您将比编写代码更频繁地阅读代码。无需保存击键。

将每个复选框作为saveMenu方法的单独参数之前。如果您使用列表,则可以将其重写为

public void saveMenu(int type, int budget, int date, List<Integer> preferences) {
     ...
     int offset = 4; // 1 = type, 2 = budget, 3 = date, 4 = first prefence
     for(int i = 0; i < 5; ++i) {
        if(i < preferences.size()) {
            ps.setLong(i + offset, preferences.get(i));
        } else {
            ps.setLong(i + offset, 0L);
        }
     }
}

再次,你可以使用一个常数作为阈值5.此外,我不能真正告诉你为什么在变量是整数时使用setLong

saveMenu方法中,即使在访问数据库或保存菜单时可能出错,也应该注意数据库连接已关闭。