簡體   English   中英

如何在Java中將單選按鈕的值插入數據庫?

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

我在我的設計中有2個JRadioButtons,分別是Male和Female。在源代碼結尾,我聲明了一個私有的String變量,命名為性別。然后在我指定的Jradiobutton action操作事件中,

在jradio button1操作中執行了事件sex =“ Male”

在jradio button2中,操作執行了事件sex =“ Female”

在我的數據庫中,我有一個名為“性別”的列,所以我需要知道的是,當我單擊表單中的“添加”按鈕時,需要向數據庫中插入選定的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();
        }            
    }

使用字符。 如果選擇了男性,則在數據庫中插入M,否則輸入F

您可以使用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 + "')");  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM