1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| public class CartDaoImpl implements CartDao { @Override public List<Cart> listCart(String userId) { List<Cart> list = new ArrayList<>(); try{ Connection con = MySqlUtil.getConn(); String sql = "Select * from cart,food,business where cart.foodId = food.foodId and cart.businessId = business.businessId and cart.userId = ?"; PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, userId); ResultSet rs = pst.executeQuery(); while(rs.next()){ Cart cart = new Cart(); cart.setCartId(rs.getInt("cartId")); cart.setUserId(rs.getString("userId")); cart.setQuantity(rs.getInt("quantity")); cart.setFoodId(rs.getInt("foodId")); cart.setBusinessId(rs.getInt("businessId"));
cart.setFood(new Food(rs.getInt("foodId"),rs.getString("foodName"),rs.getString("foodExplain"),rs.getString("foodImg"),rs.getDouble("foodPrice"),rs.getInt("businessId"),rs.getString("remarks"))); cart.setBusiness(new Business(rs.getInt("businessId"),rs.getString("businessName"),rs.getString("businessAddress"),rs.getString("businessExplain"),rs.getString("businessImg"),rs.getInt("orderTypeId"),rs.getDouble("starPrice"),rs.getDouble("deliveryPrice"),rs.getString("remarks")));
list.add(cart); } MySqlUtil.close(rs,pst,con); } catch (Exception e) { e.printStackTrace(); } return list; }
@Override public int addCart(Cart cart) { int ret=0; try{ Connection con = MySqlUtil.getConn(); String sql ="INSERT INTO cart(businessId, userId, foodId)values(?,?,?)"; PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, String.valueOf(cart.getBusinessId())); pst.setString(2, cart.getUserId()); pst.setString(3, String.valueOf(cart.getFoodId())); ret = pst.executeUpdate();
MySqlUtil.close(pst, con); return ret;
} catch (Exception e) { e.printStackTrace(); } return 0; }
@Override public int updateCart(Cart cart) { int ret; try{ Connection con = MySqlUtil.getConn(); String sql = "UPDATE cart SET quantity = ? where businessId = ? and userId = ? and foodId =? and quantity = ?"; PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1,cart.getQuantity()); pst.setInt(2,cart.getBusinessId()); pst.setString(3,cart.getUserId()); pst.setInt(4,cart.getFoodId()); ret = pst.executeUpdate();
MySqlUtil.close(pst, con); return ret;
}catch (Exception e) { e.printStackTrace(); } return 0; }
@Override public int removeCart(Cart cart) { int ret; try{ Connection con = MySqlUtil.getConn(); String sql = "DELETE FROM cart WHERE businessId = ? and userId = ? and foodId = ?"; PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1,cart.getBusinessId()); pst.setString(2, cart.getUserId()); pst.setInt(3,cart.getFoodId()); ret = pst.executeUpdate();
MySqlUtil.close(pst, con); return ret;
} catch (Exception e) { e.printStackTrace(); } return 0; } }
|