0731 用户登录、员工管理系统 CheckLogin.jsp

package com.wff.bean;


public class Emp {
	//与数据表结构一致
    private Integer empno;
    private String ename;
    private String sex;
    private Integer age;
    private Float sal;
    private Integer deptno;
	public Emp() {
		super();
		// TODO Auto-generated constructor stub
	}
   
	public Emp(Integer empno, String ename, String sex, Integer age, Float sal, Integer deptno) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.sex = sex;
		this.age = age;
		this.sal = sal;
		this.deptno = deptno;
	}
	

	public Emp(String ename, String sex, Integer age, Float sal, Integer deptno) {
		super();
		this.ename = ename;
		this.sex = sex;
		this.age = age;
		this.sal = sal;
		this.deptno = deptno;
	}

	public Integer getEmpno() {
		return empno;
	}

	public void setEmpno(Integer empno) {
		this.empno = empno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Float getSal() {
		return sal;
	}

	public void setSal(Float sal) {
		this.sal = sal;
	}

	public Integer getDeptno() {
		return deptno;
	}

	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}

	
   @Override
   public String toString() {
	return empno+"\t"+ename+"\t"+sex+"\t"+age+"\t"+sal+"\t"+deptno;
   }
    
}
package com.wff.bean;


public class User {
	private Integer id;
	private String account;
	private String pwd;
	private String nname;
	private String email;
	private String tel;
	public User() {
		super();
	}
	public User( String account, String pwd, String nname, String email, String tel) {
		super();
		this.account = account;
		this.pwd = pwd;
		this.nname = nname;
		this.email = email;
		this.tel = tel;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNname() {
		return nname;
	}
	public void setNname(String nname) {
		this.nname = nname;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	
	
}
package com.wff.dao;

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

import com.wff.bean.Emp;
import com.wff.utils.DBUtils;

public class EmpDao {
   //封装基础业务逻辑
	//增
	public int save(Emp emp) throws SQLException{
		  //声明2个核心接口
		   Connection conn = null;
		   PreparedStatement prep = null;
		   String sql = "insert into emp values(?,?,?,?,?)";
		   try {
			   //获得连接对象
			conn = DBUtils.getConnection();
			  //获得预编译对象
		     prep = conn.prepareStatement(sql);
			 //设置参数
			 prep.setString(1,emp.getEname());
			 prep.setString(2, emp.getSex());
			 prep.setInt(3, emp.getAge());
			 prep.setFloat(4, emp.getSal());
			 prep.setInt(5, emp.getDeptno());
			 //prep.setDate(x, dd);
			 //发送预编译文件,执行sql
			 return prep.executeUpdate();		 
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}finally{
			  //关闭资源
			  DBUtils.closeAll(null, prep, conn);
		}
	}
	//删,依据主键删除
	public int delete(int empno) throws SQLException{
		  //声明2个核心接口
		   Connection conn = null;
		   PreparedStatement prep = null;
		   String sql = "delete from emp where empno=?";
		   try {
			   //获得连接对象
			conn = DBUtils.getConnection();
			  //获得预编译对象
		     prep = conn.prepareStatement(sql);
			 //设置参数
			 prep.setInt(1,empno);
			 //发送预编译文件,执行sql
			 return prep.executeUpdate();		 
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}finally{
			  //关闭资源
			  DBUtils.closeAll(null, prep, conn);
		}
	}
	//改
	//只能修改数据库存在的记录
	//修改的前提是查询
	//修改应该能够修改除主键以外所有的字段
	//应该依据主键去修改
	//参数emp对象是数据库存在的记录,所以它是查询方法
	//查询出来的
	public int modify(Emp emp) throws SQLException{
		  //声明2个核心接口
		  Connection conn = null;
		  PreparedStatement prep = null;
		  //sql
		  String sql = "update emp set ename=?,sex=?,age=?,sal=?,deptno=? where empno=?";
		  try {
			//获得连接对象
			  conn = DBUtils.getConnection();
			//获得预编译对象
			  prep = conn.prepareStatement(sql);
			  //设置参数
			  prep.setString(1, emp.getEname());
			  prep.setString(2, emp.getSex());
			  prep.setInt(3, emp.getAge());
			  prep.setFloat(4, emp.getSal());
			  prep.setInt(5, emp.getDeptno());
			  prep.setInt(6, emp.getEmpno());
			  //发送预编译文件,执行sql
			  return prep.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}finally{
			 //关闭资源
			DBUtils.closeAll(null, prep, conn);
		}
	}
	
	//查(最复杂)
	//只查询一条记录,依据员工编号查询
	public Emp queryForId(int empno) throws SQLException{
		  //声明3个核心接口
		 Connection conn = null;
		 PreparedStatement prep = null;
		 ResultSet rs = null;
		 Emp emp = null;
		 //sql
		 String sql = "select * from emp where empno=?";
		 try {
			//获得连接对象
			 conn = DBUtils.getConnection();
			//获得预编译对象
			 prep = conn.prepareStatement(sql);
			 //设置参数
			 prep.setInt(1,empno);
			 //发送预编译文件,执行sql
			 //获得结果集对象
			 rs = prep.executeQuery();
			 //遍历结果集,用结果集中的数据
			 //封装对象
			 while(rs.next()){
				  emp = new Emp();
				  emp.setEmpno(rs.getInt("empno"));
				  emp.setEname(rs.getString("ename"));
				  emp.setSex(rs.getString("sex"));
				  emp.setAge(rs.getInt("age"));
				  emp.setSal(rs.getFloat("sal"));
				  emp.setDeptno(rs.getInt("deptno"));
			 }
			 return emp;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}finally{
			   //关闭资源
			DBUtils.closeAll(rs, prep, conn);
		}
	}
	//查询所有的记录
	public List<Emp> queryAll() throws SQLException{
		  //声明3个核心接口
		 Connection conn = null;
		 PreparedStatement prep = null;
		 ResultSet rs = null;
		 List<Emp> emps = null;
		 //sql
		 String sql = "select * from emp";
		 try {
			//获得连接对象
			 conn = DBUtils.getConnection();
			//获得预编译对象
			 prep = conn.prepareStatement(sql);
			 //发送预编译文件,执行sql
			 //获得结果集对象
			 rs = prep.executeQuery();
			 //遍历结果集,用结果集中的数据
			 //封装对象
			 while(rs.next()){
				  if(emps==null){
					  //循环第一次,实例化集合对象
					  emps = new ArrayList<Emp>();
				  }
				  //实例化一个员工对象
				  Emp emp = new Emp();
				  emp.setEmpno(rs.getInt("empno"));
				  emp.setEname(rs.getString("ename"));
				  emp.setSex(rs.getString("sex"));
				  emp.setAge(rs.getInt("age"));
				  emp.setSal(rs.getFloat("sal"));
				  emp.setDeptno(rs.getInt("deptno"));
				  emps.add(emp);
			 }
			 return emps;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}finally{
			   //关闭资源
			DBUtils.closeAll(rs, prep, conn);
		}
	}
}
package com.wff.dao;

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

import com.wff.bean.User;
import com.wff.utils.DBUtils;

public class UserDao {
	public User queryUserByNameAndPwd(String account,String pwd) throws SQLException {
		Connection conn = null;
		PreparedStatement prep = null;
		ResultSet rs = null;
		User users = null;
		String sql ="select * from users where account = ? and pwd = ?";
		try {
			conn = DBUtils.getConnection();
			prep = conn.prepareStatement(sql);
			prep.setString(1, account);
			prep.setString(2, pwd);
			rs = prep.executeQuery();
			while (rs.next()) {
				users = new User();
				users.setId(rs.getInt("id"));
				users.setAccount(rs.getString("account"));
				users.setPwd(rs.getString("pwd"));
				users.setNname(rs.getString("nname"));				
				users.setEmail(rs.getString("email"));
				users.setTel(rs.getString("tel"));
			}
			return users;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.closeAll(rs, prep, conn);
		}
	}
	public int saveUser(User users) throws SQLException {
		Connection conn = null;
		PreparedStatement prep = null;
		String sql = "insert into users values(?, ?, ?, ?, ?)";
		try {
			conn = DBUtils.getConnection();
			prep = conn.prepareStatement(sql);
			prep.setString(1, users.getAccount());
			prep.setString(2, users.getPwd());
			prep.setString(3, users.getNname());
			prep.setString(4, users.getEmail());
			prep.setString(5, users.getTel());		
			return prep.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.closeAll(null, prep, conn);
		}
	}
}
package com.wff.utils;

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

public class DBUtils {
		// jdbc的四个重要参数作为工具类的常量
		// 驱动字符串
		public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		// 连接字符串
		public static final String URL = "jdbc:sqlserver://localhost:1433;dataBaseName=db01";
		// 用户名
		public static final String USER = "sa";
		// 密码
		public static final String PASSWORD = "1234";

		// 在静态块中加载驱动类
		// 在类加载的时候执行的代码
		static {
			try {
				Class.forName(DRIVER);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// 获得连接对象的方法
		public static Connection getConnection() throws SQLException {
			return DriverManager.getConnection(URL, USER, PASSWORD);
		}

		// 关闭资源方法
		public static void closeAll(ResultSet rs, PreparedStatement prep, Connection conn) throws SQLException {
			try {
				if (rs != null) {
					rs.close();
				}
				if (prep != null) {
					prep.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				throw e;
			}
		}

		// 测试
		public static void main(String[] args) throws SQLException {
			Connection conn = getConnection();
			System.out.println(conn);
		}
}

Login.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
         //取出错误消息
         String error_msg = (String)request.getAttribute("error_msg");
    %>
    <h3 style='color:red'><%=error_msg==null?"":error_msg %></h3>
     <form action="DoLogin.jsp"  method="post">
        用户:<input name="username"/> <br/>
        密码:<input type="password"  name="password"/> <br/>
        <input type="submit"  value="登录"/>
     </form>
</body>
</html>

DoLogin.jsp  

<%@page import="com.wff.bean.User"%>
<%@page import="com.wff.dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 
    正确的用户名和密码: zs  123
 -->   
 <%
       //设置解码方式
         request.setCharacterEncoding("UTF-8");
       //获得Login.jsp传来的参数
       String username  = request.getParameter("username");
       String password = request.getParameter("password");
       //创建Dao对象
        UserDao dao = new UserDao();
        User user = dao.queryUserByNameAndPwd(username, password);
       
       //out.print(username+","+password);
       //判断用户名和密码是否正确
       if(user!=null){
    	   //out.print("登录成功");
    	   //跳转到主页面 Main.jsp
    	    //转发--将请求转交给另外一个jsp页面
    	    //绑定用户名
    	    //request.setAttribute("username",username);
    	     session.setAttribute("user", user);
    	     // request.setAttribute("user", user); --错误
    	     //request.getRequestDispatcher("Main.jsp").forward(request, response);    	  
    	     //使用重定向
    	     response.sendRedirect("ListEmp.jsp");
       }else{
    	   //out.print("用户名或密码错误");
    	   //绑定错误消息
    	   request.setAttribute("error_msg", "用户名或密码错误");   	   
    	   //转发回Login.jsp
    	   request.getRequestDispatcher("Login.jsp").forward(request, response);  
       }
       
 %>   

ListEmp.jsp 

<%@page import="com.wff.bean.User"%>
<%@page import="com.wff.bean.Emp"%>
<%@page import="java.util.List"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@page import="com.wff.dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
     table{
        width:600px;
        border:3px solid #ccc;
        border-collapse: collapse;
        margin:auto;
     }
     
     table th,table td{
          border:1px solid #ccc;
          
     }
     
     h3,p{
       text-align: center;
     }
     welcome{
     	
     }
     
</style>
</head>
<body>
<%@include  file="CheckLogin.jsp"%>
   <!-- 
      从数据库获得数据,渲染到页面
    -->
     <%
         EmpDao dao = new EmpDao();
          //查询出所有10条记录
          List<Emp> es =  dao.queryAll();   
          //将数据渲染到一个table
          
     %>
     
        <h3>员工管理系统</h3>
      <table>
           <tr>
              <th>编号</th>
              <th>姓名</th>
              <th>性别</th>
              <th>年龄</th>
              <th>薪资</th>
              <th>部门</th>
              <th>操作</th>
           </tr>
           <%
               for(int i=0;i<es.size();i++){
            	     //从集合中取出一个员工数据
            	     Emp e = es.get(i);
            	     //将e的数据填充为表格的一行
            	   %>  
             <tr>
              <td><%=e.getEmpno() %></td>
              <td><%=e.getEname() %></td>
              <td><%=e.getSex() %></td>
              <td><%=e.getAge() %></td>
              <td><%=e.getSal() %></td>
              <td><%=e.getDeptno() %></td>
              <td>
                   <a href="DoDelete.jsp?empno=<%=e.getEmpno()%>">删除</a>
                   <a href="ToUpdate.jsp?empno=<%=e.getEmpno()%>">修改</a>
              </td>
           </tr>
            	   <% 
               }
           %>
      </table>
     <p>
        <a href="ToAdd.jsp">添加员工</a>
     </p>
</body>
</html>

DoDelete.jsp

<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% 	Integer empno = Integer.parseInt(request.getParameter("empno"));
	EmpDao dao = new EmpDao();
	int num = dao.delete(empno);
	if(num==1){
		response.sendRedirect("ListEmp.jsp");
		return;
	}
	out.print("<h2>系统繁忙,请稍后再试</h2>");
%>

ToAdd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
	form{
	width:210px;
	margin:auto;
	}
	p{
		text-align:center;
	}
</style>
</head>
<body>
<%@include  file="CheckLogin.jsp"%>
	<p>添加员工</p>

	<form action="AddEmp.jsp" method="post">
		姓名:<input name="ename"/><br/>
		性别:<input name="sex"/><br/>
		年龄:<input name="age"/><br/>
		薪资:<input name="sal"/><br/>
		部门:<input name="deptno"/><br/>
		<input type="submit" value="添加"/>
	</form>

</body>
</html>

AddEmp.jsp

<%@page import="com.wff.bean.Emp"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String ename = request.getParameter("ename");
	String sex = request.getParameter("sex");
	Integer age = Integer.parseInt(request.getParameter("age"));
	Float sal = Float.parseFloat(request.getParameter("sal"));
	Integer deptno = Integer.parseInt(request.getParameter("deptno"));
	Emp e = new Emp(ename,sex,age,sal,deptno);
	EmpDao dao = new EmpDao();
	int num =dao.save(e);
	if(num==1){
		response.sendRedirect("ListEmp.jsp");
		return;
	}
	out.print("<h2>系统繁忙,请稍后再试</h2>");
%>

ToUpdate.jsp

<%@page import="com.wff.bean.Emp"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
	form{
	width:210px;
	margin:auto;
	}
	p{
		text-align:center;
	}
</style>
</head>
<body>
<%@include  file="CheckLogin.jsp"%>
	<%
		//获得参数empno
		Integer empno = Integer.parseInt(request.getParameter("empno"));
		EmpDao dao = new EmpDao();
		Emp e = dao.queryForId(empno);
	%>
		<p>修改员工</p>

	<form action="UpdateEmp.jsp" method="post">
	<!-- 隐藏域 -->
	<input type="hidden" name="empno" value="<%=empno%>"/>
		姓名:<input name="ename"value="<%=e.getEname()%>"/><br/>
		性别:<input name="sex" value="<%=e.getSex()%>"/><br/>
		年龄:<input name="age" value="<%=e.getAge()%>"/><br/>
		薪资:<input name="sal" value="<%=e.getSal()%>"/><br/>
		部门:<input name="deptno" value="<%=e.getDeptno()%>"/><br/>
		<input type="submit" value="修改"/>
	</form>
		

</body>
</html>
<%@page import="com.wff.bean.Emp"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	
	request.setCharacterEncoding("UTF-8");
	Integer empno = Integer.parseInt(request.getParameter("empno"));
	String ename = request.getParameter("ename");
	String sex = request.getParameter("sex");
	Integer age = Integer.parseInt(request.getParameter("age"));
	Float sal = Float.parseFloat(request.getParameter("sal"));
	Integer deptno = Integer.parseInt(request.getParameter("deptno"));
	Emp e = new Emp(empno,ename,sex,age,sal,deptno);
	EmpDao dao = new EmpDao();
	int num = dao.modify(e);
	if(num == 1){
		//添加成功---重定向回主页面
		response.sendRedirect("ListEmp.jsp");
		return;
	}else{
		out.print("<h2>系统繁忙,请稍后再试</h2>");
	}
%>

CheckLogin.jsp

<%@page import="com.wff.bean.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<style>
	#welcome{
		text-align:right;
	}
</style>
    <%
    	User u = (User)session.getAttribute("user");
    if(u==null){
    	request.setAttribute("error_msg", "请先登录");
    	request.getRequestDispatcher("Login.jsp").forward(request, response);
    	return;
    }else{
    	
    }
 %>
  <p id="welcome">欢迎<%=u.getName() %></p>

-------------------------------------------------------------------------------------------------------------------------------

CheckLogin.jsp 

<%@page import="com.wff.bean.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<style>
	#welcome{
		text-align:right;
	}
</style>
    <%
    	User u = (User)session.getAttribute("user");
    if(u==null){
    	request.setAttribute("error_msg", "请先登录");
    	request.getRequestDispatcher("Login.jsp").forward(request, response);
    	return;
    }else{
    	
    }
 %>
  <p id="welcome">欢迎<%=u.getName() %></p>

Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
         //取出错误消息
         String error_msg = (String)request.getAttribute("error_msg");
    %>
    <h3 style='color:red'><%=error_msg==null?"":error_msg %></h3>
     <form action="DoLogin.jsp"  method="post">
        用户:<input name="username"/> <br/>
        密码:<input type="password"  name="password"/> <br/>
        <input type="submit"  value="登录"/>
     </form>
</body>
</html>

DoLogin.jsp

<%@page import="com.wff.bean.User"%>
<%@page import="com.wff.dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 
    正确的用户名和密码: zs  123
 -->   
 <%
       //设置解码方式
         request.setCharacterEncoding("UTF-8");
       //获得Login.jsp传来的参数
       String username  = request.getParameter("username");
       String password = request.getParameter("password");
       //创建Dao对象
        UserDao dao = new UserDao();
        User user = dao.queryUserByNameAndPwd(username, password);
       
       //out.print(username+","+password);
       //判断用户名和密码是否正确
       if(user!=null){
    	   //out.print("登录成功");
    	   //跳转到主页面 Main.jsp
    	    //转发--将请求转交给另外一个jsp页面
    	    //绑定用户名
    	    //request.setAttribute("username",username);
    	     session.setAttribute("user", user);
    	     // request.setAttribute("user", user); --错误
    	     //request.getRequestDispatcher("Main.jsp").forward(request, response);    	  
    	     //使用重定向
    	     response.sendRedirect("ListEmp.jsp");
       }else{
    	   //out.print("用户名或密码错误");
    	   //绑定错误消息
    	   request.setAttribute("error_msg", "用户名或密码错误");   	   
    	   //转发回Login.jsp
    	   request.getRequestDispatcher("Login.jsp").forward(request, response);  
       }
       
 %>   

ListEmp.jsp

<%@page import="com.wff.bean.User"%>
<%@page import="com.wff.bean.Emp"%>
<%@page import="java.util.List"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@page import="com.wff.dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
     table{
        width:600px;
        border:3px solid #ccc;
        border-collapse: collapse;
        margin:auto;
     }
     
     table th,table td{
          border:1px solid #ccc;
          
     }
     
     h3,p{
       text-align: center;
     }
     welcome{
     	
     }
     
</style>
</head>
<body>
<%@include  file="CheckLogin.jsp"%>
   <!-- 
      从数据库获得数据,渲染到页面
    -->
     <%
         EmpDao dao = new EmpDao();
          //查询出所有10条记录
          List<Emp> es =  dao.queryAll();   
          //将数据渲染到一个table
          
     %>
     
        <h3>员工管理系统</h3>
      <table>
           <tr>
              <th>编号</th>
              <th>姓名</th>
              <th>性别</th>
              <th>年龄</th>
              <th>薪资</th>
              <th>部门</th>
              <th>操作</th>
           </tr>
           <%
               for(int i=0;i<es.size();i++){
            	     //从集合中取出一个员工数据
            	     Emp e = es.get(i);
            	     //将e的数据填充为表格的一行
            	   %>  
             <tr>
              <td><%=e.getEmpno() %></td>
              <td><%=e.getEname() %></td>
              <td><%=e.getSex() %></td>
              <td><%=e.getAge() %></td>
              <td><%=e.getSal() %></td>
              <td><%=e.getDeptno() %></td>
              <td>
                   <a href="DoDelete.jsp?empno=<%=e.getEmpno()%>">删除</a>
                   <a href="ToUpdate.jsp?empno=<%=e.getEmpno()%>">修改</a>
              </td>
           </tr>
            	   <% 
               }
           %>
      </table>
     <p>
        <a href="ToAdd.jsp">添加员工</a>
     </p>
</body>
</html>

DoDelete.jsp

<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% 	Integer empno = Integer.parseInt(request.getParameter("empno"));
	EmpDao dao = new EmpDao();
	int num = dao.delete(empno);
	if(num==1){
		response.sendRedirect("ListEmp.jsp");
		return;
	}
	out.print("<h2>系统繁忙,请稍后再试</h2>");
%>

ToUpdate.jsp

<%@page import="com.wff.bean.Emp"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
	form{
	width:210px;
	margin:auto;
	}
	p{
		text-align:center;
	}
</style>
</head>
<body>
<%@include  file="CheckLogin.jsp"%>
	<%
		//获得参数empno
		Integer empno = Integer.parseInt(request.getParameter("empno"));
		EmpDao dao = new EmpDao();
		Emp e = dao.queryForId(empno);
	%>
		<p>修改员工</p>

	<form action="UpdateEmp.jsp" method="post">
	<!-- 隐藏域 -->
	<input type="hidden" name="empno" value="<%=empno%>"/>
		姓名:<input name="ename"value="<%=e.getEname()%>"/><br/>
		性别:<input name="sex" value="<%=e.getSex()%>"/><br/>
		年龄:<input name="age" value="<%=e.getAge()%>"/><br/>
		薪资:<input name="sal" value="<%=e.getSal()%>"/><br/>
		部门:<input name="deptno" value="<%=e.getDeptno()%>"/><br/>
		<input type="submit" value="修改"/>
	</form>
		

</body>
</html>

UpdateEmp.jsp

<%@page import="com.wff.bean.Emp"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	
	request.setCharacterEncoding("UTF-8");
	Integer empno = Integer.parseInt(request.getParameter("empno"));
	String ename = request.getParameter("ename");
	String sex = request.getParameter("sex");
	Integer age = Integer.parseInt(request.getParameter("age"));
	Float sal = Float.parseFloat(request.getParameter("sal"));
	Integer deptno = Integer.parseInt(request.getParameter("deptno"));
	Emp e = new Emp(empno,ename,sex,age,sal,deptno);
	EmpDao dao = new EmpDao();
	int num = dao.modify(e);
	if(num == 1){
		//添加成功---重定向回主页面
		response.sendRedirect("ListEmp.jsp");
		return;
	}else{
		out.print("<h2>系统繁忙,请稍后再试</h2>");
	}
%>

ToAdd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
	form{
	width:210px;
	margin:auto;
	}
	p{
		text-align:center;
	}
</style>
</head>
<body>
<%@include  file="CheckLogin.jsp"%>
	<p>添加员工</p>

	<form action="AddEmp.jsp" method="post">
		姓名:<input name="ename"/><br/>
		性别:<input name="sex"/><br/>
		年龄:<input name="age"/><br/>
		薪资:<input name="sal"/><br/>
		部门:<input name="deptno"/><br/>
		<input type="submit" value="添加"/>
	</form>

</body>
</html>

AddEmp.jsp

<%@page import="com.wff.bean.Emp"%>
<%@page import="com.wff.dao.EmpDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String ename = request.getParameter("ename");
	String sex = request.getParameter("sex");
	Integer age = Integer.parseInt(request.getParameter("age"));
	Float sal = Float.parseFloat(request.getParameter("sal"));
	Integer deptno = Integer.parseInt(request.getParameter("deptno"));
	Emp e = new Emp(ename,sex,age,sal,deptno);
	EmpDao dao = new EmpDao();
	int num =dao.save(e);
	if(num==1){
		response.sendRedirect("ListEmp.jsp");
		return;
	}
	out.print("<h2>系统繁忙,请稍后再试</h2>");
%>

猜你喜欢

转载自blog.csdn.net/Fern2018/article/details/81301820