[英]Why the second product quantity override the first product quantity in java?
[英]update quantity of product in database with java
我正在建立一個超市系統,我想在用戶銷售該產品時更新庫存數量,我寫了這個 function `
public String setqty(String bar){
String qty = "SELECT SUM(QTY) FROM sales_product WHERE barcode = '"+bar+"'";
int quantity = Integer.parseInt(qty);
String sql = "update product set Qty = Qty - " + quantity + " where Barcode = " + bar;
String str = (String) db.database.getTableData(sql).items[0][0];
return str;
}`
但是當我嘗試運行它時,我得到了這些錯誤
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "SELECT SUM(QTY) FROM sales_product WHERE barcode= '12345'"
我該如何解決該錯誤
根據您的代碼,您正在使用 Integer.parseInt 解析 sql 字符串。 您需要在兩者之間調用數據庫
String qtySQL = "SELECT SUM(QTY) FROM sales_product WHERE barcode = '"+bar+"'";
String qty = (String) db.database.getTableData(qtySQL).items[0][0] //Now holding the value of sum(qty), if I got the syntax right
int quantity = Integer.parseInt(qty);
String sql = "update product set Qty = Qty - " + quantity + " where Barcode = " + bar;
String str = (String) db.database.getTableData(sql).items[0][0];
return str;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.