my question is:- When i have multiple JRadioButtons it only selects the last one to be put into my database table. I have tried alot and i can't figure out why it only insets the last RadioButton. Even when i click a different button no matter what it always selects the last One which is Gluten free Crust. Please help me out.Here is my part of the code that has to do with this Questions.
JButton btnFinish = new JButton("Finish");
btnFinish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
close();
String query = "insert into Exam2 (FullName, Address, Phonenumber, PizzaSize, CrustType, Toppings, Quantity) values (?,?,?,?,?,?,?)";
java.sql.PreparedStatement pst= con.prepareStatement(query);
pst.setString(1, txtName.getText());
pst.setString(2, txtAddress.getText());
pst.setString(3, txtNumber.getText());
pst.setString(4, rdbtnSmall.getText());
pst.setString(4, rdbtnMedium.getText());
pst.setString(4, rdbtnLarge.getText());
pst.setString(5, rdbtnHandTossed.getText());
pst.setString(5, rdbtnHandmadePan.getText());
pst.setString(5, rdbtnBrooklynStyle.getText());
pst.setString(5, rdbtnGlutenFreeCrust.getText());
pst.setString(6, rdbtnPepperoni.getText());
pst.setString(6, rdbtnItalianSausage.getText());
pst.setString(6, rdbtnBeef.getText());
pst.setString(6, rdbtnHam.getText());
pst.setString(6, rdbtnBacon.getText());
pst.setString(6, rdbtnOlives.getText());
pst.setString(6, rdbtnMushrooms.getText());
pst.setString(6, rdbtnOnions.getText());
pst.setString(7, textField.getText());
pst.execute();
close();
}catch(Exception i) {
System.err.println("Exception: " + i.getMessage());
JOptionPane.showMessageDialog(null, "There has been an error connecting to the database");
}
}
});
JSeparator separator = new JSeparator();
JLabel lblPizzaSizeAnd = new JLabel("Pizza Size And Crust");
lblPizzaSizeAnd.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
rdbtnSmall = new JRadioButton("Small(10\")");
rdbtnMedium = new JRadioButton("Medium(12\")");
rdbtnLarge = new JRadioButton("Large(14\")");
JSeparator separator_1 = new JSeparator();
JSeparator separator_2 = new JSeparator();
separator_2.setOrientation(SwingConstants.VERTICAL);
rdbtnHandmadePan = new JRadioButton("HandMade Pan");
rdbtnHandmadePan.setToolTipText("Two layers of cheese, toppings to the edge, and a crust that bakes up golden and crispy with a buttery taste.");
rdbtnHandTossed = new JRadioButton("Hand Tossed");
rdbtnHandTossed.setToolTipText("Garlic-seasoned crust with a rich, buttery taste.");
rdbtnBrooklynStyle = new JRadioButton("Brooklyn Style");
rdbtnBrooklynStyle.setToolTipText("Hand stretched to be big, thin, and perfectly foldable.");
rdbtnGlutenFreeCrust = new JRadioButton("Gluten Free Crust");
rdbtnGlutenFreeCrust.setToolTipText("Domino's pizza made with a Gluten Free Crust.");
group = new ButtonGroup();
group.add(rdbtnHandmadePan);
group.add(rdbtnHandTossed);
group.add(rdbtnBrooklynStyle);
group.add(rdbtnGlutenFreeCrust);
JSeparator separator_3 = new JSeparator();
JLabel lblToopings = new JLabel("Toppings");
lblToopings.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
rdbtnPepperoni = new JRadioButton("Pepperoni");
rdbtnItalianSausage = new JRadioButton("Italian Sausage");
rdbtnBeef = new JRadioButton("Beef");
rdbtnHam = new JRadioButton("Ham");
rdbtnBacon = new JRadioButton("Bacon");
rdbtnOlives = new JRadioButton("Olives");
rdbtnMushrooms = new JRadioButton("Mushrooms");
rdbtnOnions = new JRadioButton("Onions");
JLabel lblQuantity = new JLabel("Quantity");
textField = new JTextField();
textField.setColumns(10);
Thanks!!
You are assigning multiple values to a single column...
This means that only the last value you set will actually be used. You should be check which buttons are selected and only adding the selected ones...
You can use the
ButtonGroup
to more easily get the selected button from the group...And for a runnable example...
Small database example