2013-04-24 126 views
2

我想通過我的對話框在我的數據庫中插入orderId(應該自動遞增)和表號,但是當我回到MySql數據庫時,既沒有存儲orderId也沒有表號。可能是什麼問題呢? orders表具有屬性: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

+0

您將整個事物放在try/catch結構中。你可能會掩蓋一些錯誤。嘗試在異常塊中打印堆棧跟蹤以查看是否有任何事情正在進行。 – 2013-04-24 23:38:18

回答

3

你永遠不會在你的代碼中實際執行更新。你需要調用這個:

ps.executeUpdate(); 

有一個很好的例子準備陳述here

+0

感謝您的建議看起來像解決了它! – JudgementFlame 2013-04-24 23:45:00

+0

@ user2241440你可以點擊我答案旁邊的複選標記,它會讓每個人都知道,如果他們稍後偶然發現這是正確的解決方案,它也給了我一些觀點。 – 2013-04-24 23:46:12

1

如果您已經定義了orderId列自動遞增,那麼你sql查詢應該是這樣的:

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

你應該只設置了tableNum的價值:

ps.setInt(1, tableNum);

+0

好吧,你是完全正確的,它只是增加了它自己。謝謝。 – JudgementFlame 2013-04-24 23:45:56