我试图通过我的对话框在我的数据库中插入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) {}
}
答案 0 :(得分:3)
答案 1 :(得分:1)
如果您已将orderId
列定义为自动增加,那么您的sql
查询应如下所示:
String query = "Insert into orders(tableNum) values(?)";
您应该只设置tableNum值:
ps.setInt(1, tableNum);