【问题标题】:How to insert value of radio button into database in java?如何在java中将单选按钮的值插入数据库?
【发布时间】:2015-08-16 05:05:37
【问题描述】:

我的设计中有 2 个名为 Male 和 Female 的 JRadioButtons。在源代码的末尾,我声明了一个名为gender 的私有字符串变量。然后在我指定的 Jradiobutton 动作执行事件中,

在 jradio button1 中执行的动作事件 性别=“男”

在 jradio button2 中执行的动作事件 性别=“女”

在我的数据库中,我有一个名为 Gender 的列,所以我需要知道的是,当我单击表单中的“添加”按钮时,我需要将选定的 jradiobutton 值(男性或女性)插入数据库。

在我的添加按钮的操作执行事件中我已经这样做了,我不知道如何将 jradioButton 值插入到 db(我需要在 txtname 之后添加它),请告诉我这样做的方法。这是我的代码

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        int x = JOptionPane.showConfirmDialog(null,
                    "Do You Want to add this Record?");
        if (x == 0) {
            Connection c = DBconnect.connect();
            Statement s = (Statement) c.createStatement();
            s.executeUpdate("INSERT into employee VALUES('"
                + txtEmpId.getText() + "','" + txtName.getText()+"')");        
        } catch (Exception e) {
            e.printStackTrace();
        }            
    }

【问题讨论】:

  • 性别的数据库字段类型是什么?
  • 我已将其作为 varchar(10)
  • 然后插入gender字符串值

标签: java jdbc netbeans-8 jradiobutton


【解决方案1】:

您可以使用 JRadioButton 的 isSelected() 和 getValue() 方法。

if(rdbGenderM.isSelected()) 
        gender="Male";
else if(rdbGenderF.isSelected()) 
        gender="Female";

假设已选择性别,

s.executeUpdate("INSERT into employee VALUES('"
                + txtEmpId.getText() + "','" + txtName.getText() + "','" + gender + "')");  

【讨论】:

  • 如果其他部分需要在单选按钮的 actionPerformed 事件中或在 executUpdate 查询之前放置?我能确切地知道吗
  • 添加按钮的actionPerformed事件中的executeUpdate语句之前。
【解决方案2】:

使用字符。 如果选择了男性,则在 DB 中插入 M,否则为 F

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多