我在数据库中增加购买产品的数量时有点麻烦。数据库中更新条目总数
有人告诉我看看这个addProduct()方法,看看我可能会去写购买(int quantityPurchased)方法。
这里是代码:
public void addProduct(String desc, int qty, double price) throws SQLException {
try (Connection conn = SimpleDataSource.getConnection()) {
try (PreparedStatement stat = conn.prepareStatement(
"INSERT INTO ProductsDB (Product_Code, Description, Quantity, Price) VALUES (?, ?, ?, ?)")) {
stat.setString(1, productCode);
stat.setString(2, desc);
stat.setInt(3, qty);
stat.setDouble(4, price);
stat.execute();
}
}
}
,这就是我应该完成:
/**
* Increases the quantity of product when we've purchased products to
* replenish our supply.
*
* @param number
* the count of products purchased.
* @throws SQLException
* - on any database error
*/
public void purchased(int qtyPurchased) throws SQLException {
// TODO: Update the ProductsDB table's quantity for this
// object's product code.
}
使用SQL UPDATE语句 –
学习一些SQL,然后尝试解决问题 – Blip
的addProduct命令()方法基本上是给你你的答案... –