并使用Sqlite或MySQL作为后台数据库来存储商品信息。同时,需要开发Servlet,并实现以下核心功能: 商品的添加、修改、删除和查询; 订单的创建、查询和管理; 用户注册、登录和个人信息管理。的代码
时间: 2024-06-10 17:05:08 浏览: 136
使用JSP+Servlet实现商品管理功能,后台为数据库,功能包括商品信息浏览、商品信息详情、商品信息删除及修改。
5星 · 资源好评率100%
好的,您的第一个问题是关于商品信息存储和核心功能实现代码的。这个问题需要开发者使用Java编写,结合Servlet技术实现。以下是一个简单的示例代码,供您参考:
// 导入必要的依赖
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
// 商品管理Servlet
public class ProductManagementServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn;
// 初始化方法,连接数据库
@Override
public void init() throws ServletException {
String url = "jdbc:sqlite:/path/to/your/database.db";
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// 摧毁方法,关闭数据库连接
@Override
public void destroy() {
try {
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 处理请求方法
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String action = request.getParameter("action");
// 添加商品
if ("add".equals(action)) {
String name = request.getParameter("name");
double price = Double.parseDouble(request.getParameter("price"));
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO products(name, price) VALUES (?, ?)");
stmt.setString(1, name);
stmt.setDouble(2, price);
stmt.executeUpdate();
session.setAttribute("message", "添加成功!");
} catch (SQLException e) {
e.printStackTrace();
session.setAttribute("message", "添加失败!");
}
response.sendRedirect("index.jsp");
return;
}
// 修改商品
if ("edit".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
double price = Double.parseDouble(request.getParameter("price"));
try {
PreparedStatement stmt = conn.prepareStatement("UPDATE products SET name=?, price=? WHERE id=?");
stmt.setString(1, name);
stmt.setDouble(2, price);
stmt.setInt(3, id);
stmt.executeUpdate();
session.setAttribute("message", "修改成功!");
} catch (SQLException e) {
e.printStackTrace();
session.setAttribute("message", "修改失败!");
}
response.sendRedirect("index.jsp");
return;
}
// 删除商品
if ("delete".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM products WHERE id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
session.setAttribute("message", "删除成功!");
} catch (SQLException e) {
e.printStackTrace();
session.setAttribute("message", "删除失败!");
}
response.sendRedirect("index.jsp");
return;
}
// 查询商品
if ("search".equals(action)) {
String keyword = request.getParameter("keyword");
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM products WHERE name LIKE ? ORDER BY id DESC");
stmt.setString(1, "%" + keyword + "%");
ResultSet rs = stmt.executeQuery();
session.setAttribute("products", rs);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("search.jsp");
return;
}
// 创建订单
if ("create".equals(action)) {
int productId = Integer.parseInt(request.getParameter("product_id"));
String username = (String) session.getAttribute("username");
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO orders(product_id, username) VALUES (?, ?)");
stmt.setInt(1, productId);
stmt.setString(2, username);
stmt.executeUpdate();
session.setAttribute("message", "订单已创建!");
} catch (SQLException e) {
e.printStackTrace();
session.setAttribute("message", "订单创建失败!");
}
response.sendRedirect("order.jsp");
return;
}
// 查询订单
if ("list".equals(action)) {
String username = (String) session.getAttribute("username");
try {
PreparedStatement stmt = conn.prepareStatement("SELECT o.*, p.name AS product_name, p.price AS product_price FROM orders o LEFT JOIN products p ON o.product_id=p.id WHERE o.username=? ORDER BY o.id DESC");
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
session.setAttribute("orders", rs);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("order_list.jsp");
return;
}
// 删除订单
if ("delete_order".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM orders WHERE id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
session.setAttribute("message", "删除成功!");
} catch (SQLException e) {
e.printStackTrace();
session.setAttribute("message", "删除失败!");
}
response.sendRedirect("order_list.jsp");
return;
}
// 用户登录
if ("login".equals(action)) {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
session.setAttribute("username", username);
session.setAttribute("message", "登录成功!");
} else {
session.setAttribute("message", "用户名或密码错误!");
}
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("login.jsp");
return;
}
// 用户注销
if ("logout".equals(action)) {
session.invalidate();
response.sendRedirect("index.jsp");
return;
}
// 用户注册
if ("register".equals(action)) {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users(username, password) VALUES (?, ?)");
stmt.setString(1, username);
stmt.setString(2, password);
stmt.executeUpdate();
session.setAttribute("message", "注册成功!");
} catch (SQLException e) {
e.printStackTrace();
session.setAttribute("message", "注册失败!");
}
response.sendRedirect("register.jsp");
return;
}
}
}
请注意,这只是一个简单的示例,使用时需要根据具体需要进行更改。同时要注意使用安全的方式连接数据库,以及对请求进行适当的验证和过滤。
阅读全文