我正试图通过对话框在数据库中插入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) {}
}
最佳答案
您从未在代码中实际执行更新。你需要这样称呼:
ps.executeUpdate();
有一个很好的例子说明了准备好的语句here。