John Joe
John Joe

Reputation: 12803

insert java check box value into sql

Does anyone know how to insert the check box value into sql? All the check boxes value will correspond in one column of the field(Preferences) and will be inserted when the process button has clicked. The coding are as below:

User.java

JButton btnNewButton = new JButton("Process");
        btnNewButton.setBounds(360, 296, 89, 23);
        contentPane.add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String place=null;
                String a=(String)comboBox.getSelectedItem().toString();
                String b=(String)comboBox_1.getSelectedItem().toString();
                String day=(String)comboBox_2.getSelectedItem().toString();

                if(chckbxLei.isSelected())
                {
                    place=String.valueOf(chckbxLei.getText());

                }
                if(chckbxAdv.isSelected())
                {
                    place=String.valueOf(chckbxAdv.getText());

                }
                if(chckbxHis.isSelected())
                {
                    place=String.valueOf(chckbxHis.getText());

                }
                if(chckbxOut.isSelected())
                {
                    place=String.valueOf(chckbxOut.getText());

                }
                if(chckbxFAK.isSelected())
                {
                    place=String.valueOf(chckbxFAK.getText());

                }
                Case ca= new Case();
                try {
                    ca.addPlace(a,b,day,place);
                    LoginGUI um= new LoginGUI();
                    um.setVisible(true);

                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


    }       
    });

Case.java

 public void addPlace( String t, String k, String z,String h) throws Exception{

        DatabaseConnection db=new DatabaseConnection();
        Connection connect=db.getConnection();

        String sql="Insert into menu(Type,Budget,Day,Preferences)VALUES (?,?,?,?)";

        PreparedStatement ps=connect.prepareStatement(sql);
        ps.setString(1,t);
        ps.setString(2,k);
        ps.setString(3,z);
        ps.setString(4,h);
        ps.executeUpdate();
        connect.close();
        ps.close();
    }

Everything working fine actually just I need some guidance on how to store multiple check box values in a single column in a MySQL DB. I really need help seriously . Your help would be greatly appreciated :)

Upvotes: 2

Views: 10862

Answers (1)

John Joe
John Joe

Reputation: 12803

It solved by using this way :)

 String valuesOfCheckBox = "";
  if (chckbxLei.isSelected()) {
  valuesOfCheckBox += chckbxLei.getText() + " ";
  }
  if (chckbxAdv.isSelected()) {
  valuesOfCheckBox += chckbxAdv.getText() + " ";
  }

Upvotes: 2

Related Questions