I am trying to insert data to a table (order_details) from another table (cart). When I am executing this, It insert data twice in database. I can't figure why it happens. Can anybody help me? Thanks in advance.
CartDAO
public boolean insertCart(String username)
{
try
{
sql = "INSERT INTO order_details (username, product_code, product_name, qty, product_price, product_pic, order_status)"
+"SELECT username, product_code, product_name, qty, product_price, product_pic, order_status FROM cart WHERE username = ? AND order_status = 1";
con = DbConnection.getConnection();
pst = con.prepareStatement(sql);
pst.setString(1, username);
pst.executeUpdate();
DbConnection.close();
return true;
}catch(ClassNotFoundException | SQLException ex)
{
Logger.getLogger(CartDAO.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
CartWS
public boolean insertOrderDetails(String username)
{
CartDAO dao = new CartDAO();
return dao.insertCart(username);
}
"SELECT username, product_code, product_name, qty, product_price, product_pic, order_status FROM cart WHERE username = ? AND order_status = 1"query and check how many records it returns