gpt4 book ai didi

java - 将java复选框值插入sql

转载 作者:行者123 更新时间:2023-11-29 02:21:09 24 4
gpt4 key购买 nike

有谁知道如何将复选框值插入到sql中?所有复选框的值将对应于字段(首选项)的一列,并在单击处理按钮时插入。编码如下:

用户.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();
}

实际上一切正常,只是我需要一些指导,了解如何在 MySQL 数据库的单个列中存储多个复选框值。我真的很需要帮助。非常感谢您的帮助:)

最佳答案

用这个方法解决了:)

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

关于java - 将java复选框值插入sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com