員工管理系統

weixin_45750969發表於2020-11-14

實現功能:
員工登入
員工新增
員工列表
部門新增
1.專案結構
在這裡插入圖片描述
2.sqlserver資料庫結構
在這裡插入圖片描述
部門表
在這裡插入圖片描述

員工表
在這裡插入圖片描述
3.原始碼:
SQL語句

--建庫語句
CREATE DATABASE [MyDB3]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'MyDB2', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\MyDB2.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'MyDB2_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\MyDB2_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)

--建表語句
USE [MyDB2]
GO
/****** Object:  Table [dbo].[dept]    Script Date: 2020/11/13 17:24:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[dept](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[name] [varchar](64) NULL,
	[remark] [varchar](64) NULL,
 CONSTRAINT [PK_dept] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[users]    Script Date: 2020/11/13 17:24:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[users](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[user_name] [varchar](64) NULL,
	[pwd] [varchar](64) NULL,
	[dept_name] [varchar](64) NULL,
	[role] [varchar](64) NULL,
	[name] [varchar](64) NULL,
 CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[dept] ON 

INSERT [dbo].[dept] ([id], [name], [remark]) VALUES (1, N'管理規劃部', N'此部門為公司的最高管理部門')
INSERT [dbo].[dept] ([id], [name], [remark]) VALUES (2, N'開發部', N'負責軟體開發')
INSERT [dbo].[dept] ([id], [name], [remark]) VALUES (3, N'人事部', N'負責人事管理')
INSERT [dbo].[dept] ([id], [name], [remark]) VALUES (4, N'後勤部', N'負責後勤管理')
SET IDENTITY_INSERT [dbo].[dept] OFF
SET IDENTITY_INSERT [dbo].[users] ON 

INSERT [dbo].[users] ([id], [user_name], [pwd], [dept_name], [role], [name]) VALUES (1, N'admin', N'123456', N'管理規劃部', N'管理員', N'張三')
INSERT [dbo].[users] ([id], [user_name], [pwd], [dept_name], [role], [name]) VALUES (2, N'admin02', N'123456', N'開發部', N'職員', N'張三02')
INSERT [dbo].[users] ([id], [user_name], [pwd], [dept_name], [role], [name]) VALUES (3, N'admin03', N'123456', N'開發部', N'職員', N'張三03')
INSERT [dbo].[users] ([id], [user_name], [pwd], [dept_name], [role], [name]) VALUES (4, N'admin04', N'123456', N'開發部', N'職員', N'張三04')
SET IDENTITY_INSERT [dbo].[users] OFF

DeptAddServlet:

package org.zhangsan.action;

import java.io.IOException;
import java.io.PrintWriter;

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

import org.zhangsan.util.DBUtil;

public class DeptAddServlet extends HttpServlet {

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

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         //1.設定編碼
		request.setCharacterEncoding("utf-8");
		
		//2.獲取引數
		String name=request.getParameter("name");
		String remark=request.getParameter("remark");
		//3.響應
		
		String sql="insert into dept values(?,?)";
		
		Object[] objs={name,remark};
		
		DBUtil.update(sql, objs);
		
		response.sendRedirect("function.jsp");
	
	}

}

LoginServlet:

package org.zhangsan.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

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

import org.zhangsan.dao.UserDao;

public class LoginServlet extends HttpServlet {

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

	doPost(request, response);
	}

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

		//1.設定編碼
		request.setCharacterEncoding("utf-8");
		
		//2.接收引數
		String userName=request.getParameter("userName");
		String pwd=request.getParameter("pwd");
		
		//3.響應
		boolean flag=false;
		try {
			flag=UserDao.login(userName, pwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flag) {
			//登陸成功跳轉到功能頁面
			response.sendRedirect("function.jsp");
		} else {
            //登入失敗跳轉到登入頁面
			response.sendRedirect("index.jsp");
		}
		
	}

}

UserAddServlet:

package org.zhangsan.action;

import java.io.IOException;
import java.io.PrintWriter;

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

import org.zhangsan.util.DBUtil;

public class UserAddServlet extends HttpServlet {

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

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

		// 1.設定編碼
		request.setCharacterEncoding("utf-8");

		// 2.獲取引數
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		String name = request.getParameter("name");
		String deptName = request.getParameter("deptName");
		String role = request.getParameter("role");
		// 3.響應

		String sql = "insert into users values(?,?,?,?,?)";

		Object[] objs = { userName, pwd, deptName, role, name };

		DBUtil.update(sql, objs);

		response.sendRedirect("userList.jsp");
	}

}

User bean:

package org.zhangsan.bean;

public class User {

	private int id;
	private String userName;
	private String pwd;
	private String deptName;
	private String role;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public User(int id, String userName, String pwd, String deptName,
			String role, String name) {
		super();
		this.id = id;
		this.userName = userName;
		this.pwd = pwd;
		this.deptName = deptName;
		this.role = role;
		this.name = name;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	
}

UserDao:

package org.zhangsan.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.zhangsan.bean.User;
import org.zhangsan.util.DBUtil;

public class UserDao {

	public static boolean login(String userName, String pwd)
			throws SQLException {
		boolean flag = false;
		// 1.獲取連線
		Connection conn = DBUtil.getConn();
		// 2.準備SQL語句
		String sql = "select * from users where user_name=? and pwd= ?";
		// 3.準備ps
		PreparedStatement ps = conn.prepareStatement(sql);
		// 4.給sql語句中的?賦值
		ps.setString(1, userName);
		ps.setString(2, pwd);

		// 5.執行SQL語句
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			flag = true;
		} else {
			flag = false;
		}
		// 6.關閉資源
		DBUtil.close(conn, ps, rs);
		// 7.返回結果
		return flag;
	}

	public static ArrayList<User> getUsers() {

		ArrayList<User> users = new ArrayList<User>();

		// 1.獲取連線
		Connection conn = DBUtil.getConn();
		// 2.準備SQL語句
		String sql = "select * from users";
		// 3.準備ps
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			// 4.執行SQL語句
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();

				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("user_name"));
				user.setDeptName(rs.getString("dept_name"));
				user.setRole(rs.getString("role"));
				user.setName(rs.getString("name"));
				users.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// 5.關閉資源
			DBUtil.close(conn, ps, rs);	
		}

		// 6.返回結果
		return users;
	}
}

DBUtil工具類

package org.zhangsan.util;

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

public class DBUtil {

	// 1:載入驅動

	static {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 2:連線資料庫
	public static Connection getConn() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=MyDB3", "sa",
					"1");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;

	}

	// 3:關閉資源
	public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {

		try {
			if (conn != null)
				conn.close();
			if (ps != null)
				ps.close();
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 4:更新 新增 刪除的操作
	public static int update(String sql, Object[] objs) {
		int count = 0;
		// 1,獲取連線
		Connection conn = getConn();

		// 2.準備ps
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);

			// 3.給SQL語句中的?賦值
			for (int i = 0; i < objs.length; i++) {
				ps.setObject(i + 1, objs[i]);
			}
			// 4.執行SQL語句
			count = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//5.關閉資源
			close(conn, ps, null);
		}
        //6.返回sql的執行的結果
		return count;

	}

}

部門新增介面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'deptAdd.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<form action="deptAdd" method="post">
			部門名稱:
			<input type="text" name="name" />
			<br />
			備註說明:
			<input type="text" name="remark" />
			<br />
			<input type="submit" value="新增" />

		</form>
	</body>
</html>

功能function介面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
  </head>
  
  <body>
<a href="userList.jsp">員工列表</a>
<a href="deptAdd.jsp">新增部門</a>
  </body>
</html>

登入介面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
  </head>
  
  <body>
  <form action="login" method="post">
  賬戶:<input type="text" name="userName"/><br/>
  密碼:<input type="text" name="pwd"/><br/>
  <input type="submit"  value="登入"/>
  
  </form>
  </body>
</html>

使用者新增介面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'userAdd.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<form action="userAdd" method="post">
			登入賬號:
			<input type="text" name="userName" id="userNameInput" />
			<br />
			登入密碼:
			<input type="text" name="pwd" id="pwdInput" />
			<br />
			真實姓名:
			<input type="text" name="name" id="nameInput" />
			<br />
			所在部門:
			<input type="text" name="deptName" id="deptNameInput" />
			<br />
			使用者角色:
			<input type="text" name="role" id="roleInput" />
			<br />
			<input type="submit" value="新增" />
		</form>
		<script>

    //驗證輸入框是否為空
    function check(InputId) {
        var value = document.getElementById(InputId).value;
        if (value == "" || value == undefined) {
            return false;
        } else {
            return true
        }
    }

    //驗證登入賬號是否為空
    function checkUserName() {
        check('userNameInput');
    }

    //驗證登入密碼是否為空
    function checkPwd() {
        check('pwdInput');
    }

    //驗證真實姓名是否為空
    function checkName() {
        check('nameInput');
    }

    //判斷表單內容是否驗證通過
    function add() {
        if (checkUserName() && checkPwd() && checkName()) {
            return true;
        } else {
            return false;
        }
    }
</script>
	</body>
</html>

使用者列表介面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.zhangsan.bean.User"%>
<%@page import="org.zhangsan.dao.UserDao"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
	</head>

	<body>
		<table>
			<%
				ArrayList<User> users = UserDao.getUsers();
				request.setAttribute("users", users);
			%>
			<tr>
				<th>
					員工編號
				</th>
				<th>
					所在部門
				</th>
				<th>
					使用者角色
				</th>
				<th>
					登入賬號
				</th>
				<th>
					真實姓名
				</th>
			</tr>
			<c:forEach items="${users}" var="user">
				<tr>
					<td>
						${user.id }
					</td>
					<td>
						${user.deptName }
					</td>
					<td>
						${user.role }
					</td>
					<td>
						${user.userName }
					</td>
					<td>
						${user.name }
					</td>
				</tr>
			</c:forEach>
		</table>
		<a href="userAdd.jsp">新增員工</a>
	</body>
</html>

web配置介面:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>org.zhangsan.action.LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>DeptAddServlet</servlet-name>
    <servlet-class>org.zhangsan.action.DeptAddServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>UserAddServlet</servlet-name>
    <servlet-class>org.zhangsan.action.UserAddServlet</servlet-class>
  </servlet>



  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>DeptAddServlet</servlet-name>
    <url-pattern>/deptAdd</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>UserAddServlet</servlet-name>
    <url-pattern>/userAdd</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4.執行效果
員工登入頁面
在這裡插入圖片描述

員工新增頁面
在這裡插入圖片描述

員工列表頁面
在這裡插入圖片描述

部門新增頁面
在這裡插入圖片描述

相關文章