使用mvc模式,实现订单的增删改查,包括list.jsp.listServlet,listDao
时间: 2023-09-17 18:11:57 浏览: 136
好的,我会根据您的要求来回答问题,以下是使用MVC模式实现订单的增删改查的代码:
list.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>订单列表</title>
</head>
<body>
<h1>订单列表</h1>
<table>
<tr>
<th>订单号</th>
<th>订单名称</th>
<th>订单金额</th>
<th>操作</th>
</tr>
<c:forEach items="${orders}" var="order">
<tr>
<td>${order.orderId}</td>
<td>${order.orderName}</td>
<td>${order.orderAmount}</td>
<td>
<a href="updateServlet?orderId=${order.orderId}">修改</a>
<a href="deleteServlet?orderId=${order.orderId}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="add.jsp">添加订单</a>
</body>
</html>
```
listServlet:
```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("/listServlet")
public class ListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OrderDao orderDao = new OrderDao();
List<Order> orders = orderDao.findAll();
request.setAttribute("orders", orders);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}
```
listDao:
```java
package com.example.dao;
import com.example.model.Order;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class OrderDao {
private static final String URL = "jdbc:mysql://localhost:3306/order_db?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
public List<Order> findAll() {
List<Order> orders = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql = "SELECT * FROM orders";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Order order = new Order();
order.setOrderId(rs.getInt("orderId"));
order.setOrderName(rs.getString("orderName"));
order.setOrderAmount(rs.getDouble("orderAmount"));
orders.add(order);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, rs);
}
return orders;
}
public void add(Order order) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql = "INSERT INTO orders(orderName, orderAmount) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, order.getOrderName());
pstmt.setDouble(2, order.getOrderAmount());
pstmt.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, rs);
}
}
public void update(Order order) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql = "UPDATE orders SET orderName = ?, orderAmount = ? WHERE orderId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, order.getOrderName());
pstmt.setDouble(2, order.getOrderAmount());
pstmt.setInt(3, order.getOrderId());
pstmt.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, rs);
}
}
public void delete(int orderId) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
String sql = "DELETE FROM orders WHERE orderId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, orderId);
pstmt.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, rs);
}
}
private void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上就是使用MVC模式实现订单的增删改查的代码,希望能够帮助到您。
阅读全文