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>");
%>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章