插入语句无法正常工作

问题描述:

我想通过我的对话框在我的数据库中插入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

你永远不会在你的代码中实际执行更新。你需要调用这个:

ps.executeUpdate(); 

有一个很好的例子准备陈述here

+0

感谢您的建议看起来像解决了它! – JudgementFlame 2013-04-24 23:45:00

+0

@ user2241440你可以点击我答案旁边的复选标记,它会让每个人都知道,如果他们稍后偶然发现这是正确的解决方案,它也给了我一些观点。 – 2013-04-24 23:46:12

如果您已经定义了orderId列自动递增,那么你sql查询应该是这样的:

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

你应该只设置了tableNum的价值:

ps.setInt(1, tableNum);

+0

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