0

I have four JCheckboxes.

I can select all four options, or three, or two or at least one.

How do I get the values stored in one single column of the MySQL database?

See my code below:

String al = "";
        if(foreign.isSelected()){
            al = foreign.getText();
        }else if(travel.isSelected()){
             al2 = travel.getText();
        }else if(dang.isSelected()){
            al = dang.getText();
        }else if(med.isSelected()){
            al = med.getText();
        }

    try{    
        String sql = "INSERT INTO employee (EmployeeID, FirstName, LastName, FlightInCharge, Allowances, Position, Salary) VALUES (?,?,?,?,?,?,?)";
        PreparedStatement stt = con.prepareStatement(sql);

        stt.setInt(1, id1);
        stt.setString(2, fn);
        stt.setString(3, ln);
        stt.setString(4, fg);
        stt.setString(5, al + al2);
        stt.setString(6, po);
        stt.setDouble(7, sa);


        stt.execute();
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
yetin
  • 61
  • 11

1 Answers1

0

I don't understand why you declared al2 variable. Your code seems good and I don't understand what your exact problem is but consider these changes:

String al = "";
if(foreign.isSelected()){
   al = al + foreign.getText();
}
if(travel.isSelected()){
   al = al + travel.getText();
}
if(dang.isSelected()){
   al = al + dang.getText();
}
if(med.isSelected()){
   al = al + med.getText();
}
...
stt.setString(5, al);

remember when you load data from database you must decode this column and set value for your four fields, and to be able to do that, you can use delimiter in al variable or you can choose unique values for these fields so that you can determine to which field a value corresponds.

If they are check boxes then they have only true or false value so you must use delimiter like f;f;f;t which means the firs three ('foreign' , 'travel' and 'dang') are false and 'med' is true, or you can set column value to 'med:t' which means only 'med' is true and others are false. (You have to update code for inserting 'delimiters')

Notice that storing values of each field in a separate column enables you to query your database more elegantly and probably faster for queries like finding all rows with 'foreign = true' condition.

Dandelion
  • 744
  • 2
  • 13
  • 34
  • al2 was a typo.. what does the al = al + foreign.getText() do ? What if I check all the four checkboxes? Can all the values of all 4 checkboxes be entered in only one single column of the table? How can I achieve that ? – yetin Apr 11 '15 at 17:40
  • Nope.. It stores only one value of one of the checkboxes into the column. does not work – yetin Apr 11 '15 at 18:11
  • No it completely determines all variables. But after reading value from database you must parse it. For example 't:t:f:t' and 'FtTtMt' means completely the same and many other codings are possible. – Dandelion Apr 11 '15 at 18:17
  • Sorry! I cannot do that. If anything is unclear, mention so that if possible I explain further. – Dandelion Apr 11 '15 at 18:39
  • The thing is that when I try your codes, it stores only one value of the checkboxes despite 3 checkboxes are checked. – yetin Apr 11 '15 at 18:47
  • Sorry, "else"s where redundant. – Dandelion Apr 11 '15 at 19:08