I want to update data but it deletes instead of updates (delete and insert is good)
pro_price: bigint, pro_pic: Text
<code> public int updateProduct(int id, Product newProduct) {
Connection conn = DBConnection.getConnection();
int count;
try {
String sql = "UPDATE Product SET pro_id=?, pro_name=?, pro_quan=?, pro_price=?, pro_pic=?, pro_des=?, cat_id=? WHERE pro_id=?";
// String sql = "INSERT INTO Product (pro_id, pro_name, pro_quan, pro_price, pro_pic, pro_des, cat_id) VALUES (?, ?, ?, ?, ?, ?, ?);";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, newProduct.getId());
pst.setString(2, newProduct.getName());
pst.setInt(3, newProduct.getQuantity());
pst.setLong(4, newProduct.getPrice());
pst.setString(5, newProduct.getPic());
pst.setString(6, newProduct.getDes());
pst.setInt(7, newProduct.getCaterID());
pst.setInt(8, id);
count = pst.executeUpdate();
} catch (Exception e) {
count = 0;
e.printStackTrace();
}
return count;
}
</code>
<code> public int updateProduct(int id, Product newProduct) {
Connection conn = DBConnection.getConnection();
int count;
try {
String sql = "UPDATE Product SET pro_id=?, pro_name=?, pro_quan=?, pro_price=?, pro_pic=?, pro_des=?, cat_id=? WHERE pro_id=?";
// String sql = "INSERT INTO Product (pro_id, pro_name, pro_quan, pro_price, pro_pic, pro_des, cat_id) VALUES (?, ?, ?, ?, ?, ?, ?);";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, newProduct.getId());
pst.setString(2, newProduct.getName());
pst.setInt(3, newProduct.getQuantity());
pst.setLong(4, newProduct.getPrice());
pst.setString(5, newProduct.getPic());
pst.setString(6, newProduct.getDes());
pst.setInt(7, newProduct.getCaterID());
pst.setInt(8, id);
count = pst.executeUpdate();
} catch (Exception e) {
count = 0;
e.printStackTrace();
}
return count;
}
</code>
public int updateProduct(int id, Product newProduct) {
Connection conn = DBConnection.getConnection();
int count;
try {
String sql = "UPDATE Product SET pro_id=?, pro_name=?, pro_quan=?, pro_price=?, pro_pic=?, pro_des=?, cat_id=? WHERE pro_id=?";
// String sql = "INSERT INTO Product (pro_id, pro_name, pro_quan, pro_price, pro_pic, pro_des, cat_id) VALUES (?, ?, ?, ?, ?, ?, ?);";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, newProduct.getId());
pst.setString(2, newProduct.getName());
pst.setInt(3, newProduct.getQuantity());
pst.setLong(4, newProduct.getPrice());
pst.setString(5, newProduct.getPic());
pst.setString(6, newProduct.getDes());
pst.setInt(7, newProduct.getCaterID());
pst.setInt(8, id);
count = pst.executeUpdate();
} catch (Exception e) {
count = 0;
e.printStackTrace();
}
return count;
}
Any problem with my code and how i fix it?
thank you guy.
New contributor
LGK1412 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3