连接数据库的简易购物车
时间: 2023-09-09 17:09:21 浏览: 41
好的,以下是一个简易的购物车示例,包含连接数据库的部分:
1. 创建一个数据库表,用于存储购物车信息,例如:
```sql
CREATE TABLE `cart` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_user_product` (`user_id`,`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 创建一个购物车实体类,例如:
```java
public class Cart {
private int id;
private int userId;
private int productId;
private int quantity;
public Cart() {}
public Cart(int userId, int productId, int quantity) {
this.userId = userId;
this.productId = productId;
this.quantity = quantity;
}
//省略getter和setter方法
}
```
3. 创建一个购物车数据访问对象(DAO),用于实现对购物车表的增删改查操作,例如:
```java
public class CartDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
public CartDao() {
conn = DBUtil.getConnection(); //获取数据库连接
}
//添加购物车记录
public void addCart(Cart cart) {
String sql = "INSERT INTO cart(user_id, product_id, quantity) VALUES(?, ?, ?)";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, cart.getUserId());
ps.setInt(2, cart.getProductId());
ps.setInt(3, cart.getQuantity());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs); //关闭数据库连接
}
}
//更新购物车记录
public void updateCart(Cart cart) {
String sql = "UPDATE cart SET quantity=? WHERE user_id=? AND product_id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, cart.getQuantity());
ps.setInt(2, cart.getUserId());
ps.setInt(3, cart.getProductId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs); //关闭数据库连接
}
}
//删除购物车记录
public void deleteCart(int userId, int productId) {
String sql = "DELETE FROM cart WHERE user_id=? AND product_id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
ps.setInt(2, productId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs); //关闭数据库连接
}
}
//查询购物车记录
public Cart getCart(int userId, int productId) {
String sql = "SELECT * FROM cart WHERE user_id=? AND product_id=?";
Cart cart = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
ps.setInt(2, productId);
rs = ps.executeQuery();
if (rs.next()) {
cart = new Cart();
cart.setId(rs.getInt("id"));
cart.setUserId(rs.getInt("user_id"));
cart.setProductId(rs.getInt("product_id"));
cart.setQuantity(rs.getInt("quantity"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs); //关闭数据库连接
}
return cart;
}
}
```
4. 在Servlet中实现加入购物车的逻辑,例如:
```java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取用户ID和商品ID
int userId = Integer.parseInt(request.getParameter("userId"));
int productId = Integer.parseInt(request.getParameter("productId"));
//查询购物车中是否已经存在该商品
CartDao cartDao = new CartDao();
Cart cart = cartDao.getCart(userId, productId);
if (cart == null) { //购物车中不存在该商品,添加一条新记录
cart = new Cart(userId, productId, 1);
cartDao.addCart(cart);
} else { //购物车中已经存在该商品,将数量加1
cart.setQuantity(cart.getQuantity() + 1);
cartDao.updateCart(cart);
}
//跳转到购物车页面
response.sendRedirect("cart.jsp");
}
```
注意:以上示例代码仅供参考,实际开发中需要根据具体情况进行修改和调整。此外,为了保证安全性,需要对SQL语句进行预编译和参数化处理,防止SQL注入攻击。