我正在尝试更新名为product的数据库中的数量列。我想要从30到29的数量更新饮料芬达。一旦我开始工作,我可以对所有其他产品做同样的事情。任何帮助将不胜感激。谢谢 这是我的DB
' 7','芬达','芬达橙',' 2','冷饮', '可口可乐' 30' < ==数量列
到目前为止,这是我的代码。
// Update Stock
public void update()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/team_project_schema?useSSL=false", "root" , "password");
String query = "UPDATE product SET Quantity = Quantity - 1 WHERE ProductName = 'Fanta' and Quantity > 0";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(7, "Fanta");
pstmt.executeUpdate();
} catch (Exception e)
{
// If we don't have a error, close the connection!
System.err.println("Exception: " + e.getMessage());
} finally
{
try
{
if (conn != null)
{
conn.close();
}
} catch (SQLException e)
{
}
}
}
package testers;
import classes.Stock;
import java.util.*;
public class StockTester
{
public static void main(String[] args)
{
// create object of Stock
Stock stock = new Stock();
// print out stock from DB
System.out.println("Reading All Products From DB... ");
System.out.println();
ArrayList <Stock> allStock = stock.getProducts();
Iterator<Stock> Iterator = allStock.iterator();
while (Iterator.hasNext())
{
Stock displayStock = Iterator.next();
System.out.println(displayStock.getProductID() + " \t" + displayStock.getProductName()
+ " \t" + displayStock.getProductDesc() + " \t" + displayStock.getSection()
+ " \t" + displayStock.getSupplierName());
}
}
}
答案 0 :(得分:0)
我这样做对我来说最简单:
date
你只需要写下新数量而不是你想要多少。 例如:29不是-1;
答案 1 :(得分:0)
您可以将productId
发送到update
方法并将其作为参数发送到Prepared Satement
。
public void update()
{
...
String query = "UPDATE product SET Quantity = Quantity - 1 WHERE ProductID = ? and Quantity > 0";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, this.productID);
pstmt.executeUpdate();
...
}
在您的测试类中,您可以像这样更新。
while (Iterator.hasNext())
{
Stock displayStock = Iterator.next();
displayStock.update();
}
答案 2 :(得分:0)
我在SQL中没有看到任何参数,因此下面的代码会失败:
pstmt.setString(7, "Fanta");
你的SQL应该看起来像:
String query = "UPDATE product SET Quantity = Quantity - ? WHERE ProductName = ?";
pstmt.setInt(1, 1); // the first parameter is the index of the variable in your SQL
pstmt.setString(2, "Fanta");
更新: 让我们假设您有一个“订单”项,它包含“LineItems”的“列表”
public void updateStockCounts(Order order) {
Connection conn = getConnection(); // get a connection to the database
conn.setAutoCommit(false); // dont update the db automatically, let me do it
String sql = "UPDATE product SET Quantity = Quantity - ? WHERE ProductName = ?";
PreparedStatement pStmt = conn.prepareStatement(sql);
for(LineItem lineItem : order.getLineItems()) {
pStmt.setInt(1, lineItem.getQuantity()); // this is the quantity sold
pStmt.setString(2, lineItem.getStockName()); // this the name of the product 'Fanta' for example
pStmt.executeUpdate();
} // go on to the next lineItem if there are any more
conn.commit(); // update all processed items now.
}
当然还有其他事情要做,例如错误检查,例如,与数据库的连接可能会中断。