简体   繁体   English

如何在Java中将单选按钮的值插入数据库?

[英]How to insert value of radio button into database in java?

i have 2 JRadioButtons called Male & Female in my design.and at the end of source code,I declared a private String variable called gender.then in in Jradiobutton action perfomed events i have assigned as this, 我在我的设计中有2个JRadioButtons,分别是Male和Female。在源代码结尾,我声明了一个私有的String变量,命名为性别。然后在我指定的Jradiobutton action操作事件中,

In jradio button1 action performed event gender="Male" 在jradio button1操作中执行了事件sex =“ Male”

In jradio button2 action performed event gender="Female" 在jradio button2中,操作执行了事件sex =“ Female”

in my database i have column called Gender,so what i need to know is i need to insert ,selected jradiobutton value(male or Female) into database when i click Add button in my form. 在我的数据库中,我有一个名为“性别”的列,所以我需要知道的是,当我单击表单中的“添加”按钮时,需要向数据库中插入选定的jradiobutton值(男性或女性)。

In my Add button's action Performed event I have done this ,I don't know how to insert jradioButton value to db(i need to add it after txtname),Please tell me a way of doing that.this is my code 在我的“添加”按钮的“操作已执行”事件中,我已经这样做了,我不知道如何将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();
        }            
    }

Use characters. 使用字符。 If male was selected, insert M in DB, else F 如果选择了男性,则在数据库中插入M,否则输入F

You can use isSelected() and getValue() methods of JRadioButton. 您可以使用JRadioButton的isSelected()和getValue()方法。

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

Assuming gender has been selected, 假设已选择性别,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在jsp中设置比较数据库值的默认单选按钮 - how to set default radio button comparing database value in jsp 如何根据从数据库中获取的值设置单选按钮 - How to set the Radio Button based on the value fetched from the database 如何从 firebase 数据库 android 检索单选按钮的值 - how to retrieve value of a radio button back from firebase database android 如何使用Java Android选择单选按钮值? - How to Choose Radio Button Value using Java Android? 如何获取单选按钮的值并在文本视图上显示(Java for android) - how to get value of radio button and display on text view (Java for android) Android:如果数据库中没有值,则隐藏单选按钮 - Android: Hide the radio button if there is no value from the Database ANDROID - 如何从单选按钮获取文本并将其插入到 sqlite 数据库? - ANDROID - how do I get text from radio button and insert it to sqlite database? 我可以在Java中为单选按钮设置值吗? - Can I set a value to a radio button in Java? 如何将单选按钮转换为字符串并将其存储到数据库中? - How to turn radio button into string and store it into the database? 如何获得唯一的用户名,电子邮件ID? 以及如何在数据库中获取单选按钮值(男性或女性)。 - How to get unique username, email id? And how to get radio button value (Male or Female) in database.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM