zh13197490940

使用JSP和sevelet做一个人事管理系统,一共分为四个板块,实体层——数据操作层——数据逻辑层——业务逻辑层!

1.实体层:就是创建变量(尽量与数据库名称一样);并且给出get和set方法。

package com.chinasofti.humanresources.human;

public class Information {
    private int id ;
    private String name ;
    private String department ;
    private String position ;
    private String tel;
    private String lervel;
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getLervel() {
        return lervel;
    }
    public void setLervel(String lervel) {
        this.lervel = lervel;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
View Code

2.创建BaseDAO层

package com.chinasofti.humanresources.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDAO {

    String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String connectionString = "jdbc:sqlserver://127.0.0.1;DatabaseName=work";
    String username = "sa";
    String userpwd = "123456";

    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rst;

    public BaseDAO() {
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void getconnection() {
        try {
            conn = DriverManager.getConnection(connectionString, username,
                    userpwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ResultSet ExecuteQuest(String sql) {
        getconnection();
        return ExecuteQuest(sql, new Object[] {});
    }

    public ResultSet ExecuteQuest(String sql, Object[] params) {
        getconnection();
        try {
            pst = conn.prepareStatement(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < params.length; i++) {
            try {

                pst.setObject(i + 1, params[i]);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            rst = pst.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rst;
    }

    public int ExecuteUpdate(String sql) {
        getconnection();
        return ExecuteUpdate(sql, new Object[] {});
    }

    public int ExecuteUpdate(String sql, Object[] params) {
        getconnection();
        int result = 0;
        try {
            pst = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < params.length; i++) {
            try {
                pst.setObject(i + 1, params[i]);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            result = pst.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    public void CloseAll() {
        try {
            if (!rst.isClosed()) {
                rst.close();
            }
            if (!pst.isClosed()) {
                pst.close();
            }
            if (!conn.isClosed()) {
                conn.close();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
View Code

3.穿件数据逻辑层,写出SQL方法

package com.chinasofti.humanresources.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.chinasofti.humanresources.human.Information;

public class InformationDAO {
    BaseDAO dao = new BaseDAO();

    // 1.增加员工信息
    public void AddInformation(Information infm) {
        String sql = "insert into [Information] ([Name] ,[Department],[Position] ,[Password] ,[Tel] ,[lervel] ) values(?,?,?,?,?,?)";
        Object[] params = new Object[] { infm.getName(), infm.getDepartment(),
                infm.getPosition(), infm.getPassword(), infm.getTel(),
                infm.getLervel() };
        dao.ExecuteUpdate(sql, params);
    }

    // 2.删除
    public void Delete(int id) {
        String sql = "delete [Information] where [id]=?";
        Object[] params = new Object[] { id };
        dao.ExecuteUpdate(sql, params);
    }

    // 3.更改
    public void Update(Information infm) {
        String sql = "update [Information] set [Name]=?,[Department]=?,[Position]=? ,[Password]=? ,[Tel]=? ,[lervel]=? where [id]=?";
        Object[] params = new Object[] { infm.getName(), infm.getDepartment(),
                infm.getPosition(), infm.getPassword(), infm.getTel(),
                infm.getLervel(),infm.getId() };
        dao.ExecuteUpdate(sql, params);
    }

    // 4.查找全部
    public List<Information> getSelect() {
        List<Information> result = new ArrayList<Information>();
        String sql = "select[id],[Name] ,[Department],[Position] ,[Password] ,[Tel] ,[lervel]  from [Information]";
        ResultSet rst = dao.ExecuteQuest(sql);
        try {
            while (rst.next()) {
                Information infm = new Information();
                infm.setId(rst.getInt(1));
                infm.setName(rst.getString(2));
                infm.setDepartment(rst.getString(3));
                infm.setPosition(rst.getString(4));
                infm.setPassword(rst.getString(5));
                infm.setTel(rst.getString(6));
                infm.setLervel(rst.getString(7));
                result.add(infm);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dao.CloseAll();
        return result;
    }

    // 5.根据name查找全部
    public Information getName(String name) {
        Information infm = null;
        Object[] params = new Object[] { name };
        String sql = "select[id],[Name] ,[Department],[Position] ,[Password] ,[Tel] ,[lervel]  from [Information]  where[name]=? ";
        ResultSet rst = dao.ExecuteQuest(sql, params);
        try {
            while (rst.next()) {
                infm = new Information();
                infm.setId(rst.getInt(1));
                infm.setName(rst.getString(2));
                infm.setDepartment(rst.getString(3));
                infm.setPassword(rst.getString(5));
                infm.setTel(rst.getString(6));
                infm.setLervel(rst.getString(7));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dao.CloseAll();
        return infm;

    }

    // 6. 查找验证用户名和密码
    public Information Testnamepwd(String name, String password) {
        Information infm = new Information();
        String sql = "select[id],[Name] ,[Department],[Position] ,[Password] ,[Tel] ,[lervel]  from [Information]  where[name]=? and [password]=?";
        Object[] params = new Object[] { name, password };
        ResultSet rst = dao.ExecuteQuest(sql, params);
        try {
            while (rst.next()) {
                infm.setId(rst.getInt(1));
                infm.setName(rst.getString(2));
                infm.setDepartment(rst.getString(3));
                infm.setPassword(rst.getString(5));
                infm.setTel(rst.getString(6));
                infm.setLervel(rst.getString(7));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return infm;
    }

    // 7.查找验证身份
    public Information Testlevel(String lervel) {
        Information infm = new Information();
        String sql = "select [lervel] from [Information] where [lervel]=?";
        Object[] params = new Object[] { lervel };
        ResultSet rst = dao.ExecuteQuest(sql, params);
        try {
            if (rst.next()) {
                infm.setLervel(rst.getString(1));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return infm;
    }
    
    
    // .根据id查找全部
        public Information getInfoByID(int i) {
            Information infm = null;
            Object[] params = new Object[] { i };
            String sql = "select[id],[Name] ,[Department],[Position] ,[Password] ,[Tel] ,[lervel]  from [Information]  where[id]=? ";
            ResultSet rst = dao.ExecuteQuest(sql, params);
            try {
                while (rst.next()) {
                    infm = new Information();
                    infm.setId(rst.getInt(1));
                    infm.setName(rst.getString(2));
                    infm.setDepartment(rst.getString(3));
                    infm.setPassword(rst.getString(5));
                    infm.setTel(rst.getString(6));
                    infm.setLervel(rst.getString(7));
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            dao.CloseAll();
            return infm;

        }

}
View Code

4.业务逻辑层,在这里进行所有的逻辑操作,各种增删改查~

a.查找

package com.chinasofti.humanresources.servlet;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasofti.humanresources.dao.InformationDAO;
import com.chinasofti.humanresources.human.Attendance;
import com.chinasofti.humanresources.human.Information;


public class InformationServlet extends HttpServlet {
    int pageSize=3;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Constructor of the object.
     */
    public InformationServlet() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         InformationDAO dao = new InformationDAO();
         List<Information> ms= dao.getSelect();
         
         //1.变量总条数,总页数,每页显示的条数
        int recordCount=ms.size();
        int pageCount=(recordCount-1)/pageSize+1;
          int pageIndex=1;
          //2.下一页
          try{
              pageSize=Integer.parseInt(request.getParameter("c"));
          }catch(Exception e)
          {}
          
          try{
              pageIndex=Integer.parseInt(request.getParameter("p"));
          }catch(Exception e)
          {}
          if(pageIndex<=1){
              pageIndex=1;
          }
          if(pageIndex>pageCount){
              pageIndex=pageCount;
          }
          
          List<Information> ms2=new ArrayList<Information>();
          
          for(int i=(pageIndex-1)*pageSize;i<pageIndex*pageSize&&i<ms.size();i++){
              ms2.add(ms.get(i));
          }
          
          request.setAttribute("pageIndex", pageIndex);
          request.setAttribute("pageCount", pageCount);
          request.setAttribute("recordCount", recordCount);
          request.setAttribute("pageSize", pageSize);

         request.setAttribute("Model", ms2);
         request.getRequestDispatcher("../informationselect.jsp").forward(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
View Code

b.添加

package com.chinasofti.humanresources.servlet;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.chinasofti.humanresources.dao.InformationDAO;
import com.chinasofti.humanresources.human.Information;


@SuppressWarnings("serial")
public class InformationAdd extends HttpServlet {


    public InformationAdd() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("../informationadd.jsp").forward(request, response);
        
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String department=request.getParameter("department");
        String position=request.getParameter("position");
        String password=request.getParameter("password");
        String tel=request.getParameter("tel");
        String lervel=request.getParameter("lervel");
        
        InformationDAO dao = new InformationDAO();
        Information inform = new Information();
        
        inform.setName(name);
        inform.setDepartment(department);
        inform.setPosition(position);
        inform.setPassword(password);
        inform.setTel(tel);
        inform.setLervel(lervel);
        
        dao.AddInformation(inform);
       // request.getRequestDispatcher("../servlet/InformationServlet").forward(request, response);
        request.getRequestDispatcher("../informationselect.jsp").forward(request, response); 
        
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
View Code

c.删除

package com.chinasofti.humanresources.servlet;

import java.io.IOException;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasofti.humanresources.dao.InformationDAO;
import com.chinasofti.humanresources.human.Information;


@SuppressWarnings("serial")
public class InformationDelete extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public InformationDelete() {
        super();
    }


    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        int i=Integer.parseInt(id);
        //1.通过dao方法找出这个ID的信息
        InformationDAO dao = new InformationDAO();
        Information info = dao.getInfoByID(i);
        //2.将这个信息放到表格当中去
        request.setAttribute("Model", info);
        //3.跳转到删除表单
        request.getRequestDispatcher("../informationdelete.jsp").forward(request, response);
            }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *     
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
            int id = Integer.parseInt(request.getParameter("id"));
            InformationDAO dao = new InformationDAO();
            dao.Delete(id);
            response.sendRedirect("./InformationServlet");

    }

    public void init() throws ServletException {
        // Put your code here
    }

}
View Code

d.更改

package com.chinasofti.humanresources.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chinasofti.humanresources.dao.InformationDAO;
import com.chinasofti.humanresources.human.Information;

public class InformationUpdate extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public InformationUpdate() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        int i=Integer.parseInt(id);
        //1.通过dao方法找出这个ID的信息
        InformationDAO dao = new InformationDAO();
        Information info = dao.getInfoByID(i);
        //2.将这个信息放到表格当中去
        request.setAttribute("Model", info);
        //3.跳转到更改表单
        request.getRequestDispatcher("../informationupdate.jsp").forward(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //4.取出更改表单的数据
        String name=request.getParameter("name");
        String department=request.getParameter("department");
        String position=request.getParameter("position");
        String password=request.getParameter("password");
        String tel=request.getParameter("tel");
        String lervel=request.getParameter("lervel");
        int id = Integer.parseInt(request.getParameter("id"));
       //5.写出dao方法与之前定义的information里面的set方法
        InformationDAO dao = new InformationDAO();
        Information inform = new Information();
        //6.将获取到的数据放到information中
        inform.setName(name);
        inform.setDepartment(department);
        inform.setPosition(position);
        inform.setPassword(password);
        inform.setTel(tel);
        inform.setLervel(lervel);
        inform.setId(id);
        //7.通过DAO 层的update方法,更改信息
        dao.Update(inform);
    
        request.getRequestDispatcher("../informationselect.jsp").forward(request, response); 
        
        
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
View Code

 

posted on 2016-06-13 19:23  花花世界花花姐  阅读(12)  评论(0编辑  收藏  举报

分类:

技术点:

相关文章: