【发布时间】:2020-08-12 08:41:27
【问题描述】:
我正在尝试根据同一个数据库中的另一个值但不同的表来更改我在数据库中的表中的一个值。第一个表称为订单,第二个表称为购买供应。我想要做的是我想通过从“orders”表中减去名为 quantity_ordered 的列中的值来更改“buysupply”表中的 sumquantity 的值。我尝试编写一些查询,但它不起作用,它不断弹出错误。如果您知道解决方案,请告诉我。代码也在下面
private void DispatchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String type = txttype.getSelectedItem().toString();
String name = txtname.getText();
String quantity = txtquantity.getText();
String dispatch_row = txtdispatch.getText();
String statusDispatched = "Dispatched";
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
// Focus on this
String template = "UPDATE orders SET status = '%s' WHERE id = %s";
String template2 = "UPDATE buysupply SET sumquantity = sumquantity - %s WHERE id = %s";
String quantity_ordered = "quantity_ordered FROM orders";
pst = con1.prepareStatement(String.format(template, statusDispatched, dispatch_row));
pst.executeUpdate();
pst1 = con1.prepareStatement(String.format(template2, quantity_ordered , dispatch_row));
pst1.executeUpdate();
// Look on top
JOptionPane.showMessageDialog(null, "Item has been dispatched");
// To update the newly recorded data to the table
table_update();
// Set the textfields to empty upon button click
txttype.setSelectedIndex(-1);
txtname.setText("");
txtquantity.setText("");
txtdispatch.setText("");
txttype.requestFocus();
} catch (ClassNotFoundException | SQLException ex) {
JOptionPane.showMessageDialog(null, "Quantity or Dispatch field is not an integer, Please try again.");
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}
}
// This code is in another class file
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
String template = "SELECT SUM(quantity) as sumquantity FROM buysupply WHERE itemtype IN ('Plastic gloves', 'Rubber gloves')";
PreparedStatement pst = con1.prepareStatement(template);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
glovesum = rs.getString("sumquantity");
} else {
System.out.print("Query didn't return any results");
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(stock.class.getName()).log(Level.SEVERE, null, ex);
}
【问题讨论】:
-
“我想通过从 orders 表中减去名为 quantity_ordered 的列来更改订单中 sumquantity 的值。”这是什么意思?
sumquantity和quantity_ordered在同一个orders表中? -
不,sumquantity 在 buysupply 表中,quantity_ordered 在 orders 表中
-
对不起,我写得不好请看帖子 Agn
-
不要存储从其他值计算的值。改为创建视图,您将避免数据不一致!
-
您好,我不知道如何创建视图,我需要尽快解决这个问题,我很快就会有截止日期