JAVA-EE模板方法模式实现动态修改部门信息------计算机网络经典

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);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值