JAVA web案例 :人力资源系统

com.tinstu.dao

EmpDao.java

package com.tinstu.dao;

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

import com.tinstu.pojo.Emp;
import com.tinstu.utils.GetConnetion;

public class EmpDao {
	//查询数据公共方法
	public ArrayList findEmpBySql(String sql) throws SQLException{
		ArrayList<Emp> empList = new ArrayList<Emp>();
  		Connection conn = GetConnetion.getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		while(rs.next()) {
			Emp emp = new Emp();
			emp.setId(rs.getInt("id"));
			emp.setName(rs.getString("name"));
			emp.setAge(rs.getInt("age"));
			emp.setJob(rs.getString("job"));
			emp.setHireDate(rs.getDate("hireDate"));
			emp.setSalary(rs.getDouble("salary"));
			emp.setDeptName(rs.getString("deptName"));
			empList.add(emp);
		}
		conn.close();
		stmt.close();
		return empList;
	}
	//增加和删除数据公共方法
	public void updateBySql(String sql) throws SQLException{
		Connection conn = GetConnetion.getConn();
		Statement stmt = conn.createStatement();
		stmt.execute(sql);
		conn.close();
		stmt.close();
	}
}

 

 

com.tinstu.pojo

Emp.java

package com.tinstu.pojo;

import java.sql.Date;

public class Emp {
   private int id ;
   private String name;
   private int age;
   private String job;
   private Date hireDate;
   private double salary;
   private String deptName;
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 int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getJob() {
	return job;
}
public void setJob(String job) {
	this.job = job;
}
public Date getHireDate() {
	return hireDate;
}
public void setHireDate(Date hireDate) {
	this.hireDate = hireDate;
}
public double getSalary() {
	return salary;
}
public void setSalary(double salary) {
	this.salary = salary;
}
public String getDeptName() {
	return deptName;
}
public void setDeptName(String deptName) {
	this.deptName = deptName;
}
   
   
}

 

 

com.tinstu.service

EmpService.java

package com.tinstu.service;

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

import com.tinstu.dao.EmpDao;
import com.tinstu.pojo.Emp;
import com.tinstu.utils.Condition;

public class EmpService {
	EmpDao ed = new EmpDao();
	//查询员工
	public ArrayList<Emp> findEmps(Condition cd) throws SQLException {
		ArrayList<Emp> empsList = new ArrayList<Emp>();
		String sql="select  * from emp where 1 = 1";
		System.out.println("查询条件:"+cd.id +"-"+cd.name+"-"+cd.deptName);
		if(cd.id!=null) {
			sql=sql+" and id="+cd.id;
		}
		if(cd.name!=null) {
			sql=sql+" and name="+cd.name;
		}
		if(cd.deptName!=null) {
			sql=sql+" and deptName="+cd.deptName;
		}
		System.out.println("查询的sql语句:"+sql);
		empsList = ed.findEmpBySql(sql);
		return empsList;
	}
	//增加员工
	public void addEmp(Emp emp) throws SQLException {
		String sql = "insert into emp(name,age,job,hireDate,salary,deptName) values('"+emp.getName()+"',"+emp.getAge()+",'"+emp.getJob()+"','"+emp.getHireDate()+"',"+emp.getSalary()+",'"+emp.getDeptName()+"')";
		System.out.println("增加员工的sql语句:"+sql);
		ed.updateBySql(sql);
	}
	//删除员工
	public void deleEmp(int id) throws SQLException{
		String sql = "delete from emp where id="+id;
		System.out.println("查询的sql语句:"+sql);
		ed.updateBySql(sql);
	}

}

 

 

com.tinstu.until

condition.java

package com.tinstu.utils;

public class Condition {
	public String id;
	public String name;
	public String deptName;
	public Condition(String id, String name, String deptNname) {
		this.id = id;
		this.name = name;
		this.deptName = deptName;
	}
}

getConnetion.java

 

package com.tinstu.utils;

import java.sql.Connection;
import java.sql.DriverManager;

public class GetConnetion {
	
	
	private static String address="jdbc:mysql://localhost:3306/emp?useSSL=false&serverTimezone=UTC"; 
	private static String dataBaseName="root"; 
	private static String dataBasePwd="root";
	
	public static Connection getConn() {
		Connection conn =null;
		
			try {
				//加载数据库jdbc引擎
				Class.forName("com.mysql.cj.jdbc.Driver");
				//获取数据库的链接对象
				conn = DriverManager.getConnection(address,dataBaseName,dataBasePwd);
			} catch (Exception e) {
				System.out.println("链接失败");
				e.printStackTrace();
			}
		return conn;
	}
}

 

 

emp_add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.tinstu.service.EmpService" %>
<%@ page import="com.tinstu.utils.Condition" %>
<%@ page import="com.tinstu.pojo.Emp" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Date" %>>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	EmpService es = new EmpService();
	String name = request.getParameter("name");
	int age = Integer.parseInt(request.getParameter("age")); //需要转换int
	String job = request.getParameter("job");
	String deptName = request.getParameter("deptName");
	double salary = Double.parseDouble(request.getParameter("salary"));
	Date hireDate = Date.valueOf(request.getParameter("hireDate")); //字符串转sql中日期型
	
	Emp emp = new Emp();
	emp.setName(name);
	emp.setAge(age);
	emp.setJob(job);
	emp.setDeptName(deptName);
	emp.setSalary(salary);
	emp.setHireDate(hireDate);
	
	es.addEmp(emp);
	out.print("<h1>添加成功</h1>");
	out.print("<a href='/RenLiZiYuan/emp_index.jsp'>返回首页</a>");
	
	%>
	
</body>
</html>

 

 

emp_addPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	%>
	<!--头部-->
	<div style="background-color: cornflowerblue; color: white; width: 100%;height:80px;">
		<h1 style="line-height: 80px; margin-left:30px;">51人力资源系统</h1>
	</div>
		
	<!--菜单-->
	<table style="border:1px solid cornflowerblue ; width: 100%; ">
		<tr>
			<td width="10%"><a href="http://127.0.0.1:8080/unit10">首页</a></td>
			<td width="10%"><a href="http://127.0.0.1:8080/unit10">添加员工</a></td>
			<td width="10%"><a href="http://127.0.0.1:8080/unit10">其他</a></td>
			<td></td>
			<td></td>
			<td width="50%"></td>
		</tr>
	</table>
		
	<!--增加表单-->
	<form action="http://127.0.0.1:8080/RenLiZiYuan/emp_add.jsp" method="get" style="margin-left: 50px; margin-top: 30px;">
			姓名:<input type="text"  name="name"/><br /><br />
			年龄:<input type="number"  name="age"/><br /><br />
			职位:<input type="text"  name="job"/><br /><br />
			部门:<select style="width: 160px; height: 25px;" name="deptName">
					<option value="董事会">董事会</option>
					<option value="销售部">销售部</option>
					<option value="研发部">研发部</option>
					<option value="行政部">行政部</option>
					<option value="财务部">财务部</option>
				</select><br /><br />
			入职日期:<input type="date" name="hireDate"/><br /><br />
			工资:<input type="number"  name="salary"/><br /><br />
			<input type="submit" value="确定添加"/>
			<input type="reset" value="取消"/>
	</form>
	
</body>
</html>

 

emp_del.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.tinstu.service.EmpService" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	EmpService es = new EmpService();
	int id = Integer.parseInt(request.getParameter("id"));
    es.deleEmp(id);
    out.print("删除成功");
    out.print("<a href='/RenLiZiYuan/emp_index.jsp'>返回首页</a>");
	%>
</body>
</html>

 

emp_index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.tinstu.service.EmpService" %>
<%@ page import="com.tinstu.utils.Condition" %>
<%@ page import="com.tinstu.pojo.Emp" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	EmpService es = new EmpService();
	//获取条件参数
	String idCondition = request.getParameter("id");
	String nameCondition = request.getParameter("name");
	String deptNameCondition = request.getParameter("deptName");
	//构造条件对象
	Condition cd = new Condition(idCondition,nameCondition,deptNameCondition);
	ArrayList<Emp> empsList = es.findEmps(cd);  //获取条件查询结构
	
	request.setAttribute("empList",empsList);  //将获取的数据保存到作用域中
	%>
	<!--头部-->
	<div style="background-color: cornflowerblue; color: white; width: 100%;height:80px;">
		<h1 style="line-height: 80px; margin-left:30px;">RLZY</h1>
	</div>
		
	<!--菜单-->
	<table style="border:1px solid cornflowerblue ; width: 100%; ">
		<tr>
			<td width="10%"><a href="http://127.0.0.1:8080/RenLiZiYuan/emp_index.jsp">首页</a></td>
			<td width="10%"><a href="http://127.0.0.1:8080/RenLiZiYuan/emp_addPage.jsp">添加员工</a></td>
			<td width="10%"><a href="http://127.0.0.1:8080/unit10">其他</a></td>
			<td></td>
			<td></td>
			<td width="50%"></td>
		</tr>
	</table>
		
	<!--条件查询-->
	<br /><form action="http://127.0.0.1:8080/unit10" method="get" style="width: 100%;">
		条件查询: 员工id  <input type="number"  style="width: 100px;" name="id"/>
		姓名  <input type="text"  style="width: 100px;" name="name"/>
		部门  <select style="width: 160px; height: 25px;" name="deptName">
				<option value="">所有</option>
				<option value="董事会">董事会</option>
				<option value="销售部">销售部</option>
				<option value="研发部">研发部</option>
				<option value="行政部">行政部</option>
				<option value="财务部">财务部</option>
		</select>
		<input type="submit" value="查询员工"/>
		<input type="reset" value="清空"/>
		
	</form><br />
		
	<!--数据显示-->
	<table border="1px" style="width:100%">
		<tr>
				<td>员工id</td>
				<td>姓名</td>
				<td>年龄</td>
				<td>职位</td>
				<td>入职日期</td>
				<td>薪资</td>
				<td>部门</td>
				<td>操作</td>
		</tr>
		<c:forEach items="${requestScope.empList }" var="emp">
		   <tr>
		     <td>${ emp.id }</td>
		     <td>${ emp.name }</td>
		     <td>${ emp.age }</td>
		     <td>${ emp.job }</td>
		     <td>${ emp.hireDate }</td>
		     <td>${ emp.salary }</td>
		     <td>${ emp.deptName }</td>
		     <td><a href="http://127.0.0.1:8080/RenLiZiYuan/emp_del.jsp?id=${ emp.id }"> 删除 </a></td>
		   </tr>
		</c:forEach>
	
		
	</table>
</body>
</html>

 

前端截图

 

阅读剩余
THE END