Java代码
ResultSet rs=null;
String sqlquery="";
Statement st;
String var="";
double sum = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mypos","root","");
Statement stmt=(Statement)conn.createStatement();
String prod = fld_product.getText();
int quant = Integer.parseInt(fld_quantity.getText());
float price = Float.parseFloat(fld_price.getText());
String query = "INSERT INTO Cart (Product, Quantity, Price) VALUES ('"+prod+"','"+quant+"','"+price+"');";
stmt.executeUpdate(query);
while(rs.next()){
String product = rs.getString("Product");
sum = Integer.parseInt(rs.getString("quantity")) + quant;
if(prod.equals(product)){
String sql2 = "UPDATE Product SET quantity = "+sum+" WHERE product = '"+prod+"'";
stmt.executeUpdate(sql2);
} else{
String sql1 = "INSERT INTO Product (product,quantity,price) " + "VALUES ('"+prod+"', '"+quant+"', '"+price+"')";
stmt.executeUpdate(sql1);
}
}
conn.close();
JOptionPane.showMessageDialog(null, "PRODUCT SAVED!","SUCCESS", JOptionPane.PLAIN_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage() ,"ERROR!", 1);
}
this.loadCartToJTable();
this.clearFields();
}
我不知道发生了什么。这个代码当我输入焦炭作为产品然后打印价格然后当我输入相同的产品 - 焦炭时,行将合并,但价格和数量将总是被覆盖。