JudgementFlame
JudgementFlame

Reputation: 107

Insert statement not working properly

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

Upvotes: 2

Views: 111

Answers (2)

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

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.

Upvotes: 3

Pradeep Pati
Pradeep Pati

Reputation: 5919

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);

Upvotes: 1

Related Questions