package com.bjpowernode.oa.web.action; import com.bjpowernode.oa.utils.DBUtil; import com.bjpowernode.oa.web.Pojo.Dept; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @WebServlet(urlPatterns = {"/dept/list","/dept/detail","/dept/delete","/dept/save","/dept/edit"}) public class DeptServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String servletPath = request.getServletPath(); if("/dept/list".equals(servletPath)) { doList(request,response); } else if("/dept/detail".equals(servletPath)) { doDetail(request,response); } else if("/dept/delete".equals(servletPath)) { doDel(request,response); } else if("/dept/save".equals(servletPath)) { doSave(request,response); } else if("/dept/edit".equals(servletPath)) { doModify(request,response); } } private void doModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Integer deptno = Integer.parseInt(request.getParameter("deptno")); String dname = request.getParameter("dname"); String loc = request.getParameter("loc"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String contextPath = request.getContextPath(); int count = 0; try { connection = DBUtil.getConnection(); connection.setAutoCommit(false); String sql = "update dept set dname = ?,loc = ? where deptno = ?"; statement = connection.prepareStatement(sql); statement.setString(1,dname); statement.setString(2,loc); statement.setInt(3,deptno); count = statement.executeUpdate(); connection.commit(); } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException ex) { throw new RuntimeException(ex); } } throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } if(count == 1) { response.sendRedirect(contextPath + "/dept/list"); } else { response.sendRedirect("/Error.html"); } } private void doSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Integer deptno = Integer.parseInt(request.getParameter("deptno")); String dname = request.getParameter("dname"); String loc = request.getParameter("loc"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String contextPath = request.getContextPath(); int count = 0; try { connection = DBUtil.getConnection(); connection.setAutoCommit(false); String sql = "insert into dept(deptno,dname,loc) values(?,?,?)"; statement = connection.prepareStatement(sql); statement.setInt(1,deptno); statement.setString(2,dname); statement.setString(3,loc); count = statement.executeUpdate(); connection.commit(); } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException ex) { throw new RuntimeException(ex); } } throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } if(count == 1) { response.sendRedirect(contextPath + "/dept/list"); } else { response.sendRedirect("/Error.html"); } } private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String deptno = request.getParameter("deptno"); int count = 0; try { connection = DBUtil.getConnection(); connection.setAutoCommit(false); String sql = "delete from dept where deptno = ?"; statement = connection.prepareStatement(sql); statement.setString(1,deptno); count = statement.executeUpdate(); connection.commit(); } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException ex) { throw new RuntimeException(ex); } } throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } if(count == 1) { response.sendRedirect(request.getContextPath() + "/dept/list"); } else { response.sendRedirect(request.getContextPath() + "/Error.html"); } } private void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String deptno = request.getParameter("deptno"); try { connection = DBUtil.getConnection(); String sql = "select * from dept where deptno = ?"; statement = connection.prepareStatement(sql); statement.setString(1,deptno); resultSet = statement.executeQuery(); if(resultSet.next()) { String dname = resultSet.getString("dname"); String loc = resultSet.getString("loc"); Dept dept = new Dept(deptno,dname,loc); request.setAttribute("dept",dept); } } catch (SQLException e) { throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } String flag = request.getParameter("f"); if("d".equals(flag)) { request.getRequestDispatcher("/detail.jsp").forward(request,response); } if("m".equals(flag)) { request.getRequestDispatcher("/edit.jsp").forward(request,response); } } private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //连接数据库,查询所有部门信息 Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; List<Dept> list = new ArrayList<Dept>(); try { connection = DBUtil.getConnection(); String sql = "select * from dept"; statement = connection.prepareStatement(sql); resultSet = statement.executeQuery(); while(resultSet.next()) { String deptno = resultSet.getString("deptno"); String dname = resultSet.getString("dname"); String loc = resultSet.getString("loc"); Dept dept = new Dept(deptno,dname,loc); list.add(dept); } request.setAttribute("dept",list); request.getRequestDispatcher("/list.jsp").forward(request,response); } catch (SQLException e) { throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } } }
package com.bjpowernode.oa.web.action; import com.bjpowernode.oa.utils.DBUtil; import com.bjpowernode.oa.web.Pojo.Dept; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @WebServlet(urlPatterns = {"/dept/list","/dept/detail","/dept/delete","/dept/save","/dept/edit"}) public class DeptServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String servletPath = request.getServletPath(); if("/dept/list".equals(servletPath)) { doList(request,response); } else if("/dept/detail".equals(servletPath)) { doDetail(request,response); } else if("/dept/delete".equals(servletPath)) { doDel(request,response); } else if("/dept/save".equals(servletPath)) { doSave(request,response); } else if("/dept/edit".equals(servletPath)) { doModify(request,response); } } private void doModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Integer deptno = Integer.parseInt(request.getParameter("deptno")); String dname = request.getParameter("dname"); String loc = request.getParameter("loc"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String contextPath = request.getContextPath(); int count = 0; try { connection = DBUtil.getConnection(); connection.setAutoCommit(false); String sql = "update dept set dname = ?,loc = ? where deptno = ?"; statement = connection.prepareStatement(sql); statement.setString(1,dname); statement.setString(2,loc); statement.setInt(3,deptno); count = statement.executeUpdate(); connection.commit(); } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException ex) { throw new RuntimeException(ex); } } throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } if(count == 1) { response.sendRedirect(contextPath + "/dept/list"); } else { response.sendRedirect("/Error.html"); } } private void doSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Integer deptno = Integer.parseInt(request.getParameter("deptno")); String dname = request.getParameter("dname"); String loc = request.getParameter("loc"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String contextPath = request.getContextPath(); int count = 0; try { connection = DBUtil.getConnection(); connection.setAutoCommit(false); String sql = "insert into dept(deptno,dname,loc) values(?,?,?)"; statement = connection.prepareStatement(sql); statement.setInt(1,deptno); statement.setString(2,dname); statement.setString(3,loc); count = statement.executeUpdate(); connection.commit(); } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException ex) { throw new RuntimeException(ex); } } throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } if(count == 1) { response.sendRedirect(contextPath + "/dept/list"); } else { response.sendRedirect("/Error.html"); } } private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String deptno = request.getParameter("deptno"); int count = 0; try { connection = DBUtil.getConnection(); connection.setAutoCommit(false); String sql = "delete from dept where deptno = ?"; statement = connection.prepareStatement(sql); statement.setString(1,deptno); count = statement.executeUpdate(); connection.commit(); } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException ex) { throw new RuntimeException(ex); } } throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } if(count == 1) { response.sendRedirect(request.getContextPath() + "/dept/list"); } else { response.sendRedirect(request.getContextPath() + "/Error.html"); } } private void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String deptno = request.getParameter("deptno"); try { connection = DBUtil.getConnection(); String sql = "select * from dept where deptno = ?"; statement = connection.prepareStatement(sql); statement.setString(1,deptno); resultSet = statement.executeQuery(); if(resultSet.next()) { String dname = resultSet.getString("dname"); String loc = resultSet.getString("loc"); Dept dept = new Dept(deptno,dname,loc); request.setAttribute("dept",dept); } } catch (SQLException e) { throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } String flag = request.getParameter("f"); if("d".equals(flag)) { request.getRequestDispatcher("/detail.jsp").forward(request,response); } if("m".equals(flag)) { request.getRequestDispatcher("/edit.jsp").forward(request,response); } } private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //连接数据库,查询所有部门信息 Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; List<Dept> list = new ArrayList<Dept>(); try { connection = DBUtil.getConnection(); String sql = "select * from dept"; statement = connection.prepareStatement(sql); resultSet = statement.executeQuery(); while(resultSet.next()) { String deptno = resultSet.getString("deptno"); String dname = resultSet.getString("dname"); String loc = resultSet.getString("loc"); Dept dept = new Dept(deptno,dname,loc); list.add(dept); } request.setAttribute("dept",list); request.getRequestDispatcher("/list.jsp").forward(request,response); } catch (SQLException e) { throw new RuntimeException(e); } finally { DBUtil.close(connection,statement,resultSet); } } }
JAVA-EE模板方法模式实现动态修改部门信息------计算机网络经典
于 2023-10-14 16:24:08 首次发布