1

How I can insert multiple selected checkboxes to a database in Java of course. I added the "," but its not working only the first selected checkbox got stored.

How can I solve this?

Here is my current code:

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();}
Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
Zack Khenniba
  • 53
  • 2
  • 7
  • 2
    just as a side note: when using the results of boolean methods the `== true` is not needed – Marco Forberg Apr 15 '13 at 14:03
  • 1
    at all places, instead of `haspaper=something.getText()+",";` it should be `haspaper += something.getText()+",";`. Just add `+` before `=` – Fahim Parkar Apr 15 '13 at 14:08
  • As the text is highly localized value i would recommend some other technique. You could use a [rank function](http://en.wikipedia.org/wiki/Ranking_function), to create a scalar result that you will store in db. [Some more reading](http://stackoverflow.com/questions/9048225/java-enum-confusion-with-creating-a-bitmask-and-checking-permissions) – Damian Leszczyński - Vash Apr 15 '13 at 16:59

3 Answers3

1

You are assigning value each if condition to haspaper variable. Append value as per your logic and instead of if else put if block Like :

haspaper += value
Tushar Trivedi
  • 400
  • 1
  • 2
  • 12
1

You can capture the checked values in servlet/jsp and store in pojo/model class as a object and finally store in to the database using jdbc/hibernate...

Srinivas
  • 147
  • 5
1

Correction in your code:

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();
    }
Ravi Sharma
  • 873
  • 7
  • 24
  • This still won't work if multiple boxes are checked, only one will be stored. – Adrian Apr 22 '13 at 13:04
  • @Adrian - I think this code is just a part of main logic, only Zack knows whether this code is working for him or not. – Ravi Sharma Apr 22 '13 at 13:11
  • This code, as it is, doesn't do what the OP said they needed to do. Plain and simple. Either the code was modified heavily to work, or the OP's question was misstated. – Adrian Apr 22 '13 at 13:41
  • 1
    Would you like to post your whole code for a small mistake and when you know which part of code is related to it? I think you will not. Everyone like to make it simple and clear enough to be understood by all. – Ravi Sharma Apr 22 '13 at 13:47