在我的项目中,员工插入一个表号,选择客户订购的所有项目并将其保存到数据库中。我有三张桌子:
Employee(empId, firstname, lastname)
Orders(orderId,tableNum,empIDFK,itemIDFK,totalPrice)
Item(itemId,itemName, itemPrice)
我的问题是,如果员工在订单中放置了多个项目,则只会将最后一个项目保存在 itemIDFK 列中。如何获取员工输入的所有项目的 ID?
这是一些示例代码,大多数按钮都有类似的代码:
private void chickenbuttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
st = connection.createStatement();
String query;
query = "SELECT itemName, itemPrice FROM item WHERE itemID = '14446'";
String itemName = " ",itemPrice =" ";
ResultSet rs = st.executeQuery(query);
if(rs != null){
while(rs.next())
{
itemName = rs.getString(1);
itemPrice = rs.getString(2);
}
model.addRow(new Object[]{itemName, itemPrice});
total+= Double.parseDouble(itemPrice);
String format = formatter.format(total);
totalField.setText(format);
}
//inserts corresponding item id in itemIDFK
String query2 = "Update orders SET itemIDFK = '14446' Where tableNum = " + tableNum;
ps= connection.prepareStatement(query2);
ps.executeUpdate();
} catch (SQLException ex) {}