insert java check box value into sql
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 :)
I have 5 checkboxes and I want insert 3 checkbox values into sql. The problem is it insert only one checkbox value into sql
– John Joe
Jul 26 '15 at 9:36
check box value means true or false ? right
– Fast Snail
Jul 26 '15 at 9:37
To make it more clearly, I have edited the post
– John Joe
Jul 26 '15 at 10:09
@user5156075 you can but it's not recommended .read database normalization
– Fast Snail
Jul 26 '15 at 10:47
1 Answer
1
It solved by using this way :)
String valuesOfCheckBox = "";
if (chckbxLei.isSelected()) {
valuesOfCheckBox += chckbxLei.getText() + " ";
}
if (chckbxAdv.isSelected()) {
valuesOfCheckBox += chckbxAdv.getText() + " ";
}
Thanks for writing this answer. It was helpful. Can you please tell me what sql data type you used for the check box in your database?
– Tia
Mar 4 '16 at 21:09
I'm using
varchar
– John Joe
Mar 5 '16 at 3:47
varchar
Thanks! These codes worked fine for me and were helpful in my project!:-)
– Tia
Mar 6 '16 at 16:22
I'm glad it helped
– John Joe
Mar 6 '16 at 16:24
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what is the problem? don't you know how to get checkbox value or what?
– Fast Snail
Jul 26 '15 at 9:28