今天写极限测试写了好久,随便交一个servlet类
package com.example.limittest01; 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.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @WebServlet("/AddEmployeeServlet") public class AddEmployeeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 获取表单提交的数据 String jobID = request.getParameter("jobID"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String birthday = request.getParameter("birthday"); String department = request.getParameter("department"); String role = request.getParameter("role"); Connection connection = null; PreparedStatement preparedStatement = null; // 验证工号是否唯一 if (!isJobIDUnique(jobID)) { out.println("工号已存在,添加失败"); return; } // 如果选择的身份是经理,检查部门中是否已经有经理 if ("manager".equals(role) && isDepartmentManagerExists(department)) { out.println("所选部门已有经理,不允许添加新的经理"); return; } try { // 获取数据库连接 connection = DatabaseConnection.getConnection(); // 执行插入数据的 SQL 语句 String sql = "INSERT INTO employee (JobID, Name, Sex, Birthday, Department, Role) VALUES (?, ?, ?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, jobID); preparedStatement.setString(2, name); preparedStatement.setString(3, sex); preparedStatement.setString(4, birthday); preparedStatement.setString(5, department); preparedStatement.setString(6, role); // 执行 SQL 语句 int rowsAffected = preparedStatement.executeUpdate(); if (rowsAffected > 0) { // 插入成功 response.getWriter().print("插入成功"); } else { // 插入失败 response.getWriter().print("插入失败"); } } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); // 异常处理,这里可以根据具体情况返回不同的信息 response.getWriter().print("插入失败,发生异常"); } finally { // 关闭连接和 statement try { if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } // 插入成功 out.println("添加成功"); } // 验证工号是否唯一 private boolean isJobIDUnique(String jobID) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { // 获取数据库连接 connection = DatabaseConnection.getConnection(); // 执行查询的 SQL 语句 String sql = "SELECT COUNT(*) FROM employee WHERE JobID = ?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, jobID); // 执行查询 resultSet = preparedStatement.executeQuery(); // 获取查询结果 if (resultSet.next()) { int count = resultSet.getInt(1); // 如果 count 大于 0,说明存在相同的工号,返回 false return count == 0; } } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); // 异常处理,这里可以根据具体情况返回 false return false; } finally { // 关闭连接、statement 和 resultSet try { if (resultSet != null) { resultSet.close(); } if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } // 默认返回 true return true; } // 检查部门中是否已经有经理 private boolean isDepartmentManagerExists(String department) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { // 获取数据库连接 connection = DatabaseConnection.getConnection(); // 执行查询的 SQL 语句 String sql = "SELECT COUNT(*) FROM employee WHERE Department = ? AND Role = 'manager'"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, department); // 执行查询 resultSet = preparedStatement.executeQuery(); // 获取查询结果 if (resultSet.next()) { int count = resultSet.getInt(1); // 如果 count 大于 0,说明该部门中已经有经理,返回 true return count > 0; } } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); // 异常处理,这里可以根据具体情况返回 false return false; } finally { // 关闭连接、statement 和 resultSet try { if (resultSet != null) { resultSet.close(); } if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } // 默认返回 false return false; } }