写一个基于javaweb的鲜花贩卖系统
时间: 2024-03-12 15:44:29 浏览: 151
这里提供一个基于JavaWeb的简单鲜花贩卖系统的示例,仅供参考。
1. 首先,需要创建一个数据库,用于存储鲜花信息、订单信息等。这里我们创建一个名为 "flower_shop" 的数据库,包含两个表:
- flower 表:包含 flower_id、flower_name、flower_price、flower_image 等字段,用于存储鲜花的信息。
- order 表:包含 order_id、user_id、flower_id、order_time、order_status 等字段,用于存储订单信息。
2. 然后,创建一个 JavaWeb 项目,包含以下文件和目录:
```
|- src/
| |- com.example.controller/
| | |- FlowerController.java
| | |- OrderController.java
| |- com.example.dao/
| | |- FlowerDao.java
| | |- OrderDao.java
| |- com.example.model/
| | |- Flower.java
| | |- Order.java
| |- com.example.utils/
| | |- DBUtils.java
|- webapp/
| |- WEB-INF/
| | |- views/
| | | |- flower.jsp
| | | |- order.jsp
| | |- web.xml
| |- index.jsp
| |- login.jsp
| |- register.jsp
|- pom.xml
```
3. 在 pom.xml 中添加以下依赖:
```xml
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
```
4. 编写 Flower 类和 Order 类,用于存储鲜花信息和订单信息:
```java
package com.example.model;
public class Flower {
private int flowerId;
private String flowerName;
private double flowerPrice;
private String flowerImage;
public int getFlowerId() {
return flowerId;
}
public void setFlowerId(int flowerId) {
this.flowerId = flowerId;
}
public String getFlowerName() {
return flowerName;
}
public void setFlowerName(String flowerName) {
this.flowerName = flowerName;
}
public double getFlowerPrice() {
return flowerPrice;
}
public void setFlowerPrice(double flowerPrice) {
this.flowerPrice = flowerPrice;
}
public String getFlowerImage() {
return flowerImage;
}
public void setFlowerImage(String flowerImage) {
this.flowerImage = flowerImage;
}
}
```
```java
package com.example.model;
import java.util.Date;
public class Order {
private int orderId;
private int userId;
private int flowerId;
private Date orderTime;
private int orderStatus;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getFlowerId() {
return flowerId;
}
public void setFlowerId(int flowerId) {
this.flowerId = flowerId;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public int getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(int orderStatus) {
this.orderStatus = orderStatus;
}
}
```
5. 编写 FlowerDao 类和 OrderDao 类,用于操作数据库:
```java
package com.example.dao;
import com.example.model.Flower;
import com.example.utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class FlowerDao {
public static List<Flower> getAllFlowers() {
List<Flower> flowers = new ArrayList<>();
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM flower")) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Flower flower = new Flower();
flower.setFlowerId(rs.getInt("flower_id"));
flower.setFlowerName(rs.getString("flower_name"));
flower.setFlowerPrice(rs.getDouble("flower_price"));
flower.setFlowerImage(rs.getString("flower_image"));
flowers.add(flower);
}
} catch (SQLException e) {
e.printStackTrace();
}
return flowers;
}
public static Flower getFlowerById(int flowerId) {
Flower flower = null;
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM flower WHERE flower_id = ?")) {
stmt.setInt(1, flowerId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
flower = new Flower();
flower.setFlowerId(rs.getInt("flower_id"));
flower.setFlowerName(rs.getString("flower_name"));
flower.setFlowerPrice(rs.getDouble("flower_price"));
flower.setFlowerImage(rs.getString("flower_image"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return flower;
}
}
```
```java
package com.example.dao;
import com.example.model.Order;
import com.example.utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class OrderDao {
public static List<Order> getOrdersByUserId(int userId) {
List<Order> orders = new ArrayList<>();
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `order` WHERE user_id = ?")) {
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Order order = new Order();
order.setOrderId(rs.getInt("order_id"));
order.setUserId(rs.getInt("user_id"));
order.setFlowerId(rs.getInt("flower_id"));
order.setOrderTime(rs.getDate("order_time"));
order.setOrderStatus(rs.getInt("order_status"));
orders.add(order);
}
} catch (SQLException e) {
e.printStackTrace();
}
return orders;
}
public static void addOrder(int userId, int flowerId) {
try (Connection conn = DBUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO `order` (user_id, flower_id, order_time, order_status) VALUES (?, ?, ?, ?)")) {
stmt.setInt(1, userId);
stmt.setInt(2, flowerId);
stmt.setObject(3, new Date());
stmt.setInt(4, 0);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
6. 编写 FlowerController 类和 OrderController 类,用于处理 HTTP 请求:
```java
package com.example.controller;
import com.example.dao.FlowerDao;
import com.example.model.Flower;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/flower")
public class FlowerController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Flower> flowers = FlowerDao.getAllFlowers();
req.setAttribute("flowers", flowers);
req.getRequestDispatcher("/WEB-INF/views/flower.jsp").forward(req, resp);
}
}
```
```java
package com.example.controller;
import com.example.dao.OrderDao;
import com.example.model.Order;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/order")
public class OrderController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int userId = (int) req.getSession().getAttribute("userId");
List<Order> orders = OrderDao.getOrdersByUserId(userId);
req.setAttribute("orders", orders);
req.getRequestDispatcher("/WEB-INF/views/order.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int userId = (int) req.getSession().getAttribute("userId");
int flowerId = Integer.parseInt(req.getParameter("flowerId"));
OrderDao.addOrder(userId, flowerId);
resp.sendRedirect(req.getContextPath() + "/order");
}
}
```
7. 在 web.xml 中配置 Servlet 和 JSP:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>FlowerController</servlet-name>
<servlet-class>com.example.controller.FlowerController</servlet-class>
</servlet>
<servlet>
<servlet-name>OrderController</servlet-name>
<servlet-class>com.example.controller.OrderController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlowerController</servlet-name>
<url-pattern>/flower</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>OrderController</servlet-name>
<url-pattern>/order</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
```
8. 编写 JSP 页面,用于展示鲜花信息和订单信息:
flower.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>鲜花商店</title>
</head>
<body>
<h1>鲜花商店</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>图片</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="flower" items="${flowers}">
<tr>
<td>${flower.flowerId}</td>
<td>${flower.flowerName}</td>
<td>${flower.flowerPrice}</td>
<td><img src="${flower.flowerImage}" alt="${flower.flowerName}" width="100"></td>
<td>
<form action="${pageContext.request.contextPath}/order" method="post">
<input type="hidden" name="flowerId" value="${flower.flowerId}">
<button type="submit">购买</button>
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
order.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的订单</title>
</head>
<body>
<h1>我的订单</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>鲜花名称</th>
<th>鲜花价格</th>
<th>鲜花图片</th>
<th>下单时间</th>
<th>状态</th>
</tr>
</thead>
<tbody>
阅读全文