mercredi 5 août 2015

make the checkbox correspond to 5 columns only in sql

I have 9 checkboxes and I have limitted the selection to 5 only. The checked box will then inserted into sql. And in my sql, I have created a menu table, which has 8 columns named Type,Budget,Day, Pre1,Pre2,Pre3,Pre4 and Pre5.(Pre1,Pre2,Pre3,Pre4,Pre5 used to store checkbox value) If there are only three checke boxes are checked, the other two column will get "0". How to do that?

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();

    }




Aucun commentaire:

Enregistrer un commentaire