0

I have the following 4 JChecboxes in my form. If the user clicks all the four, or any choices of JCheckboxes how do I save the values from the checboxes in mysql database in one sigle column? When I click my Add button, it should stored all the values from the selected checkboxes Please help.. Thanks

My codes :

    foreign = new JCheckBox("Foreign");
    foreign.setFont(new Font("Tahoma", Font.BOLD, 12));
    foreign.setForeground(new Color(240, 255, 240));
    foreign.setBounds(16, 25, 97, 23);
    foreign.setOpaque(false);
    panel.add(foreign);

    travelling = new JCheckBox("Travelling");
    travelling.setFont(new Font("Tahoma", Font.BOLD, 12));
    travelling.setForeground(new Color(240, 255, 240));
    travelling.setBounds(150, 26, 97, 23);
    travelling.setOpaque(false);
    panel.add(travelling);

    danger = new JCheckBox("Danger Pay");
    danger.setFont(new Font("Tahoma", Font.BOLD, 12));
    danger.setForeground(new Color(240, 255, 240));
    danger.setBounds(16, 68, 97, 23);
    danger.setOpaque(false);
    panel.add(danger);

    medical = new JCheckBox("Medical Scheme");
    medical.setFont(new Font("Tahoma", Font.BOLD, 12));
    medical.setForeground(new Color(240, 255, 240));
    medical.setBounds(150, 69, 121, 23);
    medical.setOpaque(false);
    panel.add(medical);
        add = new JButton("Add");
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            int id1 = Integer.parseInt(id.getText());
            String fn = fname.getText();
            String ln = lname.getText();
            String po = pos.getText();
            String sa = sal.getText();
            String all = "";
                if(foreign.isSelected() && travelling.isSelected() && danger.isSelected() && medical.isSelected()){
                    all = "Foreign, Travelling, Danger, Medical";
                }else if(foreign.isSelected() && travelling.isSelected() && danger.isSelected()){
                    all = "Foreign, Travelling, Danger";
                }else if(foreign.isSelected() && travelling.isSelected()){
                    all = "Foreign, Travelling";
                }else if()


        }
    });
yetin
  • 61
  • 11

1 Answers1

0

You could save the data in a separate table. What I mean by this is that suppose you are storing the data in a table temp which is having 2 columns - id and selection. Now my suggestion is that you create 2 tables one for id and the other for selection. the selection table will have 2 columns id and selection. the id column is to be a foreign key to the id table's id column.

Now when the user having id say 01 selects "Foreign" & "Travelling", then you make 2 entries in the selection table that are 01 - Foreign and 01 - Travelling

Now when the user having id say 02 selects all the options then you are to make 4 entries in the selection table against the id of the user:

  1. 02-Foreign,
  2. 02-Travelling,
  3. 02-Danger,
  4. 02-Medical

This way you could save each selection separately.

After getting you comment

After your comment I could only suggest that you create 4 columns of boolean type in the table and store the state of each checkBox in each column.

Blip
  • 3,061
  • 5
  • 22
  • 50