hi i have a problem. i have a system to input data from a jform to a mysql database. i have made all of it except the gender radiobuttons. how do i do this? i need when register is clicked depending on which button was clicked male or female be sent to the data base. i use netbeans 7.2 and mysql 5.5 here is my code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package my.registration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author User
*/
public class RegistrationUIMenu1 extends javax.swing.JFrame{
/**
* Creates new form RegistrationUIMenu1
*/
public RegistrationUIMenu1() {
initComponents();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/autmgt","root","K13nn0r");
ResultSet rs=null;
String SQL="select * from child";
Statement st=conn.createStatement();
rs=st.executeQuery(SQL);
rs.last();
int k=rs.getInt(1)+1;
String k2=rs.getString(1)+1;
k2 = Integer.toString(k);
Lbl.setText(k2);
}catch(Exception e){}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel10 = new javax.swing.JPanel();
text3 = new javax.swing.JTextField();
jLabel17 = new javax.swing.JLabel();
text1 = new javax.swing.JTextField();
text2 = new javax.swing.JTextField();
jLabel18 = new javax.swing.JLabel();
jSeparator3 = new javax.swing.JSeparator();
jLabel19 = new javax.swing.JLabel();
radio1 = new javax.swing.JRadioButton();
radio2 = new javax.swing.JRadioButton();
jLabel20 = new javax.swing.JLabel();
jLabel21 = new javax.swing.JLabel();
text7 = new javax.swing.JTextField();
text6 = new javax.swing.JTextField();
jLabel50 = new javax.swing.JLabel();
jLabel51 = new javax.swing.JLabel();
jLabel22 = new javax.swing.JLabel();
Lbl = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel10.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel17.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
jLabel17.setText("Basic Details");
text1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
text1ActionPerformed(evt);
}
});
jLabel18.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel18.setText("Name in Full");
jLabel19.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel19.setText("Date of Birth");
buttonGroup1.add(radio1);
radio1.setText("Male");
radio1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radio1ActionPerformed(evt);
}
});
buttonGroup1.add(radio2);
radio2.setText("Female");
radio2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radio2ActionPerformed(evt);
}
});
jLabel20.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel20.setText("Age");
jLabel21.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel21.setText("Gender");
jLabel50.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel50.setText("School");
jLabel51.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel51.setText("Grade");
jLabel22.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel22.setText("Child ID");
javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
jPanel10.setLayout(jPanel10Layout);
jPanel10Layout.setHorizontalGroup(
jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel10Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel10Layout.createSequentialGroup()
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator3)
.addGroup(jPanel10Layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addGap(4, 4, 4))
.addGroup(jPanel10Layout.createSequentialGroup()
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel18)
.addComponent(jLabel20)
.addComponent(jLabel19)
.addComponent(jLabel21)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel10Layout.createSequentialGroup()
.addComponent(jLabel51)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(text7, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel10Layout.createSequentialGroup()
.addComponent(jLabel50)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 146, Short.MAX_VALUE)
.addComponent(text6, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel10Layout.createSequentialGroup()
.addComponent(jLabel22)
.addGap(140, 140, 140)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(radio1)
.addComponent(radio2)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(text3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
.addComponent(text2, javax.swing.GroupLayout.Alignment.TRAILING)))
.addComponent(text1)
.addComponent(Lbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(85, Short.MAX_VALUE))))
);
jPanel10Layout.setVerticalGroup(
jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel10Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(3, 3, 3)
.addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel22))
.addGap(35, 35, 35)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(text1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel18))
.addGap(29, 29, 29)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel20)
.addComponent(text2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel19)
.addComponent(text3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel21)
.addGroup(jPanel10Layout.createSequentialGroup()
.addComponent(radio1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(radio2)))
.addGap(44, 44, 44)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel50)
.addComponent(text6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel51)
.addComponent(text7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(73, Short.MAX_VALUE))
);
jButton2.setText("Cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton1.setText("Register");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 308, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(524, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(20, 20, 20))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(63, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
private void radio1ActionPerformed(java.awt.event.ActionEvent evt) {
String val4="Male"; // TODO add your handling code here:
}
private void radio2ActionPerformed(java.awt.event.ActionEvent evt) {
String val5="Female"; // TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/autmgt","root","K13nn0r");
Statement stmt = conn.createStatement();
String val01=Lbl.getText();
String val1=text1.getText();
String val2=text2.getText();
String val3=text3.getText();
String val4=radio1.getText();
String val5=radio2.getText();
String val6=text6.getText();
String val7=text7.getText();
String sql="INSERT INTO child VALUES("
+"'"+val01+"'"+","
+"'"+val1+"'"+","
+"'"+val2+"'"+","
+"'"+val3+"'"+","
+"'"+val4+"'"+","
+"'"+val6+"'"+","
+"'"+val7+"'"+")";
stmt.executeUpdate(sql);
}catch(Exception e){}
System.exit(0); // TODO add your handling code here:
}
private void text1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(RegistrationUIMenu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RegistrationUIMenu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RegistrationUIMenu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RegistrationUIMenu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RegistrationUIMenu1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Lbl;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel18;
private javax.swing.JLabel jLabel19;
private javax.swing.JLabel jLabel20;
private javax.swing.JLabel jLabel21;
private javax.swing.JLabel jLabel22;
private javax.swing.JLabel jLabel50;
private javax.swing.JLabel jLabel51;
private javax.swing.JPanel jPanel10;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JRadioButton radio1;
private javax.swing.JRadioButton radio2;
private javax.swing.JTextField text1;
private javax.swing.JTextField text2;
private javax.swing.JTextField text3;
private javax.swing.JTextField text6;
private javax.swing.JTextField text7;
// End of variables declaration
}
Here is a java swing example where we are going to insert three fields - name, password and gender.
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingFrame{
JTextField text;
public static void main(String[] args) throws Exception{
SwingFrame sf=new SwingFrame();
}
public SwingFrame(){
JFrame f = new JFrame("Frame in Java Swing");
f.getContentPane().setLayout(null);
JLabel lbl1 = new JLabel("Name");
final JTextField jt1=new JTextField(15);
JLabel lbl2 = new JLabel("Password");
final JPasswordField jt2=new JPasswordField(15);
JLabel lbl3 = new JLabel("Confirm Password");
final JPasswordField jt3=new JPasswordField(15);
JLabel lbl4 = new JLabel("Gender");
final JRadioButton Male,Female;
ButtonGroup radioGroup=new ButtonGroup();
Male=new JRadioButton("Male");
radioGroup.add(Male);
Female=new JRadioButton("Female");
radioGroup.add(Female);
JButton button=new JButton("Submit");
text=new JTextField(15);
Male.addActionListener(al);
Female.addActionListener(al);
lbl1.setBounds(50,50,70,30);
lbl2.setBounds(50,90,70,30);
lbl3.setBounds(50,130,70,30);
lbl4.setBounds(50,170,70,30);
button.setBounds(50,210,100,30);
jt1.setBounds(110,50,100,30);
jt2.setBounds(110,90,100,30);
jt3.setBounds(110,130,100,30);
Male.setBounds(110,170,100,30);
Female.setBounds(210,170,100,30);
text.setBounds(110,210,100,30);
text.setVisible(false);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name=jt1.getText();
String pass=jt2.getText();
String cpass=jt3.getText();
String g=text.getText();
if(name.length()>10){
JOptionPane.showMessageDialog(null,"You cannot exceed more than 10 characters in the name field");
jt1.setText("");
}
else if(!pass.equals(cpass)){
JOptionPane.showMessageDialog(null,"Your password and confirm password does not match");
jt2.setText("");
jt3.setText("");
}
else if((Male.isSelected()==false)&&(Female.isSelected()==false)){
JOptionPane.showMessageDialog(null,"Please select radio button");
}
else{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:access");
PreparedStatement st=con.prepareStatement("insert into names(name,password,gender) values(?,?,?)");
st.setString(1,name);
st.setString(2,pass);
st.setString(3,g);
int i=st.executeUpdate();
JOptionPane.showMessageDialog(null,"Data is successfully inserted into database.");
}
catch(Exception ex){}
}
}
});
f.add(lbl1);
f.add(lbl2);
f.add(lbl3);
f.add(lbl4);
f.add(jt1);
f.add(jt2);
f.add(jt3);
f.add(Male);
f.add(Female);
f.add(button);
f.add(text);
f.setSize(500,500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText(((JRadioButton) e.getSource()).getText());
i understand all that but i want to enter the values of the gender buttongroup into the database and im using mysql not access