欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

员工管理系统

程序员文章站 2022-07-12 14:29:44
...

实现功能:
员工登录
员工新增
员工列表
部门新增
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.运行效果
员工登录页面
员工管理系统

员工新增页面
员工管理系统

员工列表页面
员工管理系统

部门新增页面
员工管理系统

相关标签: jdbc java