基于JSP+servlet+JDBC开发的人物管理系统
发布人:shili8
发布时间:2024-12-24 09:30
阅读次数:0
**人物管理系统**
本系统基于JSP+S servlet+JDBC技术栈,实现了对人物信息的增删改查功能。
### 系统结构* **Servlet**:负责处理请求、响应结果* **JDBC**:用于数据库操作* **JSP**:用于展示页面### 实现步骤####1.创建数据库表
sqlCREATE TABLE person ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, age INT NOT NULL, sex VARCHAR(10) NOT NULL);
####2.编写Servlet类**PersonServlet.java**
javapackage com.example.person; import javax.servlet.ServletException; import javax.servlet. />import javax.servlet. />import javax.servlet. />import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class PersonServlet extends HttpServlet { private static final String DB_URL = "jdbc:mysql://localhost:3306/person"; private static final String USER = "root"; private static final String PASS = ""; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 列表页 showList(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 添加或修改 addOrUpdate(req, resp); } private void showList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, USER, PASS); String sql = "SELECT * FROM person"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); StringBuilder sb = new StringBuilder(); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String sex = rs.getString("sex"); sb.append("<tr><td>").append(id).append("</td><td>").append(name).append("</td><td>").append(age).append("</td><td>").append(sex).append("</td></tr>"); } req.setAttribute("list", sb.toString()); req.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(req, resp); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } private void addOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(DB_URL, USER, PASS); String name = req.getParameter("name"); int age = Integer.parseInt(req.getParameter("age")); String sex = req.getParameter("sex"); String sql = "INSERT INTO person (name, age, sex) VALUES (?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setString(3, sex); int count = pstmt.executeUpdate(); if (count >0) { req.setAttribute("msg", "添加成功!"); } else { req.setAttribute("msg", "添加失败!"); } } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } req.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(req, resp); } }
####3.编写JSP页面**list.jsp**
jsp<%@ page contentType="text/html; charset=UTF-8" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>人物列表</title> </head> <body> <h1>人物列表</h1> <table border="1"> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> ${list} </table> </body> </html>
**msg.jsp**
jsp<%@ page contentType="text/html; charset=UTF-8" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>消息</title> </head> <body> ${msg} </body> </html>
####4.配置web.xml
xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" /> xsi:schemaLocation=" /> version="2.5" xmlns=" /> <servlet> <servlet-name>personServlet</servlet-name> <servlet-class>com.example.person.PersonServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>personServlet</servlet-name> <url-pattern>/person</url-pattern> </servlet-mapping> </web-app>
### 总结本系统基于JSP+S servlet+JDBC技术栈,实现了对人物信息的增删改查功能。通过编写Servlet类、JSP页面和配置web.xml,可以完成对人物管理的基本需求。
**注意**
* 本系统仅供参考,不适合生产环境使用。
* 需要根据实际情况进行调整和优化。
*请遵守相关法律法规和规范。