2

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,

In jradio button1 action performed event gender="Male"

In jradio button2 action performed event gender="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.

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

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();
        }            
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

You can use isSelected() and getValue() methods of JRadioButton.

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 + "')");  
Surya
  • 350
  • 2
  • 11
  • if else part needed to put in actionPerformed event of radio button or just before the executUpdate query?? Can I exactly know that –  Aug 18 '15 at 03:38
  • Add it before the `executeUpdate` statement in Add button's `actionPerformed` event. – Surya Aug 18 '15 at 07:50