2

I am trying to insert the orderId(which should auto increment) and table number in my database through my dialog box, but when i refer back to MySql database neither the orderId or the table numbers are stored. What could be the problem? The orders table has the attributes: orderId, tableNum, numofGuests, itemIdFK, empIdFK.

 private void orderButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here: 
  try {
      //Dialog box asking for the table number 
String tableNumString =  JOptionPane.showInputDialog(null,"What is the table number?",
      "Your Password",JOptionPane.QUESTION_MESSAGE);

    int tableNum = Integer.parseInt(tableNumString);
    System.out.println(tableNum);
    String query = "Insert into orders(orderId,tableNum) values(?,?)";

        int order = 1;

        ps = connection.prepareStatement(query);

        ps.setInt(1, order);
        ps.setInt(2, tableNum);

        //switch to the next page after table number is inserted 
         CardLayout cl = (CardLayout)(mainpanel.getLayout());
      cl.show(mainpanel,"card3");
    } catch (SQLException | NumberFormatException ex) {}
}                                           

My Dialog box

JudgementFlame
  • 107
  • 1
  • 2
  • 9
  • You are enclosing the whole thing in a try/catch structure. You might be masking some errors. Try printing a stacktrace in the exception block to see if anything is going on. – Chris Chambers Apr 24 '13 at 23:38

2 Answers2

3

You never actually execute the update in your code. You need to call this:

ps.executeUpdate();

There's a nice example of prepared statements here.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
  • @user2241440 you can click the checkmark next to my answer, it will let everyone know that this is the correct solution if they stumble across it later, it also gives me some points. – Jean-Bernard Pellerin Apr 24 '13 at 23:46
1

If you have defined the orderId column to auto-increment, then your sql query should look like this :

String query = "Insert into orders(tableNum) values(?)";

And you should set only the tableNum value :

ps.setInt(1, tableNum);

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43