org. comp.model
Employee Model – Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package org.comp.model; import java.io.Serializable; import java.math.BigDecimal; import java.util.Objects; public class Employee implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String name; private String department; private String designation; private BigDecimal salary; public Employee() { } public Employee(Long id, String name, String department, String designation, BigDecimal salary) { super(); this.id = id; this.name = name; this.department = department; this.designation = designation; this.salary = salary; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", department=" + department + ", designation=" + designation + ", salary=" + salary + "]"; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public BigDecimal getSalary() { return salary; } public void setSalary(BigDecimal salary) { this.salary = salary; } public static long getSerialversionuid() { return serialVersionUID; } } |
Employee Controller Servlet
added org.comp.controller package is used for controller
AddEmployee.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package org.comp.controller; import java.io.IOException; import java.math.BigDecimal; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.comp.dao.EmployeeDAO; import org.comp.model.Employee; @WebServlet("/add-employee") public class AddEmployee extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String department = request.getParameter("department"); String designation = request.getParameter("designation"); String salary = request.getParameter("salary"); Employee e = new Employee(); e.setName(name); e.setDepartment(department); e.setDesignation(designation); e.setSalary(new BigDecimal(salary)); System.out.println(e); EmployeeDAO dao = new EmployeeDAO(); dao.insert(e); List<Employee> employeeList = dao.fetchAll(); request.setAttribute("employeeList", employeeList); int employeeCount = dao.count(); request.setAttribute("employeeCount", employeeCount); RequestDispatcher rd = request.getRequestDispatcher("/ListEmployees.jsp"); rd.forward(request, response); } } |
FetchEmployee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package org.comp.controller; import java.io.IOException; import java.math.BigDecimal; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.comp.dao.EmployeeDAO; import org.comp.model.Employee; @WebServlet("/update") public class FetchEmployee extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Long id=Long.valueOf( request.getParameter("id")); EmployeeDAO dao = new EmployeeDAO(); Employee employee = dao.fetch(id); System.out.println(employee); request.setAttribute("employee", employee); RequestDispatcher rd = request.getRequestDispatcher("/UpdateEmployee.jsp"); rd.forward(request, response); } } |
UpdateEmployee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package org.comp.controller; import java.io.IOException; import java.math.BigDecimal; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.comp.dao.EmployeeDAO; import org.comp.model.Employee; @WebServlet("/update-employee") public class UpdateEmployee extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Long id = Long.valueOf(request.getParameter("id")); String name = request.getParameter("name"); String department = request.getParameter("department"); String designation = request.getParameter("designation"); String salary = request.getParameter("salary"); Employee e = new Employee(); e.setId(id); e.setName(name); e.setDepartment(department); e.setDesignation(designation); e.setSalary(new BigDecimal(salary)); System.out.println(e); EmployeeDAO dao = new EmployeeDAO(); dao.update(e); List<Employee> employeeList = dao.fetchAll(); request.setAttribute("employeeList", employeeList); int employeeCount = dao.count(); request.setAttribute("employeeCount", employeeCount); RequestDispatcher rd = request.getRequestDispatcher("/ListEmployees.jsp"); rd.forward(request, response); } } |
showEmployee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package org.comp.controller; import java.io.IOException; import java.math.BigDecimal; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.comp.dao.EmployeeDAO; import org.comp.model.Employee; @WebServlet("/show-employee") public class ShowEmployee extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EmployeeDAO dao = new EmployeeDAO(); List<Employee> employeeList = dao.fetchAll(); int employeeCount = dao.count(); request.setAttribute("employeeList", employeeList); request.setAttribute("employeeCount", employeeCount); RequestDispatcher rd = request.getRequestDispatcher("/ListEmployees.jsp"); rd.forward(request, response); } } |
DeleteEmployee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package org.comp.controller; import java.io.IOException; import java.math.BigDecimal; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.comp.dao.EmployeeDAO; import org.comp.model.Employee; @WebServlet("/delete") public class DeleteEmployee extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Long id =Long.valueOf(request.getParameter("id")); EmployeeDAO dao = new EmployeeDAO(); int st = dao.delete(id); List<Employee> employeeList = dao.fetchAll(); request.setAttribute("employeeList", employeeList); RequestDispatcher rd = request.getRequestDispatcher("/ListEmployees.jsp"); rd.forward(request, response); } } |
Data Access Object
ConnectionFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package org.comp.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionFactory { public static Connection getConnection() { Connection c = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); c = DriverManager.getConnection("jdbc:mysql://localhost:3306/sspu", "sspu_userx25", "MV7GG5TV2312"); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException " + e); } catch (SQLException e) { System.out.println("SQLException " + e); } return c; } } |
EmployeeDAO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | package org.comp.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 org.comp.model.Employee; public class EmployeeDAO { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; int st;// status public int insert(Employee employee) { con = ConnectionFactory.getConnection(); try { String query = "insert into employee(name,department,designation,salary) " + "values(?,?,?,?)"; ps = con.prepareStatement(query); ps.setString(1, employee.getName()); ps.setString(2, employee.getDepartment()); ps.setString(3, employee.getDesignation()); ps.setBigDecimal(4, employee.getSalary()); st = ps.executeUpdate(); System.out.println("inserted employee " + st); } catch (Exception e) { st = -2; e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return st; } public int update(Employee employee) { con = ConnectionFactory.getConnection(); try { String query = "update employee set name=?,department=?,designation=?,salary=? where id=?"; ps = con.prepareStatement(query); ps.setString(1, employee.getName()); ps.setString(2, employee.getDepartment()); ps.setString(3, employee.getDesignation()); ps.setBigDecimal(4, employee.getSalary()); ps.setLong(5, employee.getId()); st = ps.executeUpdate(); System.out.println("Updated employee " + st); } catch (Exception e) { st = -2; e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return st; } public List<Employee> fetchAll() { List<Employee> employeeList = new ArrayList<Employee>(); con = ConnectionFactory.getConnection(); try { String query = "select * from employee order by name"; ps = con.prepareStatement(query); rs = ps.executeQuery(); while (rs.next()) { Employee employee = new Employee(); employee.setId(rs.getLong("id")); employee.setName(rs.getString("name")); employee.setDepartment(rs.getString("department")); employee.setDesignation(rs.getString("designation")); employee.setSalary(rs.getBigDecimal("salary")); employeeList.add(employee); } } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return employeeList; } public Employee fetch(long id) { Employee employee = null; con = ConnectionFactory.getConnection(); try { String query = "select * from employee where id=?"; ps = con.prepareStatement(query); ps.setLong(1, id); rs = ps.executeQuery(); while (rs.next()) { employee = new Employee(); employee.setId(rs.getLong("id")); employee.setName(rs.getString("name")); employee.setDepartment(rs.getString("department")); employee.setDesignation(rs.getString("designation")); employee.setSalary(rs.getBigDecimal("salary")); System.out.println(employee); } } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return employee; } public int delete(Long id) { con = ConnectionFactory.getConnection(); try { String query = "delete from employee where id=?"; ps = con.prepareStatement(query); ps.setLong(1, id); st = ps.executeUpdate(); System.out.println("Deleted employee " + st); } catch (Exception e) { st = -2; e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return st; } public int count() { int count = 0; con = ConnectionFactory.getConnection(); try { String query = "select count(*) from employee"; ps = con.prepareStatement(query); ResultSet rs = ps.executeQuery(); while (rs.next()) { count = rs.getInt(1); } System.out.println("Total employees " + count); } catch (Exception e) { st = -2; e.printStackTrace(); } finally { try { con.close(); } catch (SQLException ex) { ex.printStackTrace(); } } return count; } } |
HTML Pages
ListEmployees.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <%@page import="org.comp.model.Employee"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Employee List</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet"> <script </head> <body class="container-fluid"> <div class="row"> <jsp:include page="menu.jsp" /> </div> <div class="row"> <div class="col-2"></div> <div class="col-8"> <h2>List of Employees ${employeeCount}</h2> <small></small><a href="AddEmployee.jsp">Add Employee</a></small> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Name</th> <th>Department</th> <th>Designation</th> <th>Salary</th> <th>Update</th> <th>Delete</th> </tr> </thead> <tbody> <c:forEach items="${employeeList}" var="employee" varStatus="loop"> <tr> <td>${loop.index+1}</td> <td>${employee.name}</td> <td>${employee.department}</td> <td>${employee.designation}</td> <td>${employee.salary}</td> <td><a href="update?id=${employee.id}">Update</a></td> <td><a href="delete?id=${employee.id}">Delete</a></td> </tr> </c:forEach> </tbody> </table> </div> <div class="col-2"></div> </div> <div class="row"> <jsp:include page="footer.jsp"></jsp:include> </div> </body> </html> |
AddEmployee.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet"> <script </head> <body class="container-fluid"> <div class="row"> <jsp:include page="menu.jsp" /> </div> <div class="row"> <div class="col-2"></div> <div class="col-8"> <h2>Add New Employee</h2> <form id="add-employee" action="add-employee" method="post"> <div class="mb-3 mt-3"> <label for="name">Name</label> <input type="text" class="form-control" id="name" placeholder="Name" name="name" required> </div> <div class="mb-3"> <label for="department">Department</label> <input type="text" class="form-control" id="department" placeholder="Enter Department" name="department" required /> </div> <div class="mb-3 mt-3"> <label for="designation">Designation</label> <input type="text" class="form-control" id="designation" placeholder="Enter Designation" name="designation" required /> </div> <div class="mb-3"> <label for="salary">Salary</label> <input type="number" class="form-control" id="salary" placeholder="Enter Salary" name="salary" required /> </div> <button type="submit" class="btn btn-primary">Add Employee</button> </form> </div> <div class="col-2"></div> </div> <div class="row"> <jsp:include page="footer.jsp" /> </div> </body> </html> |
UpdateEmployee.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <%@page import="org.comp.model.Employee"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet"> <script </head> <body class="container-fluid"> <div class="row"> <jsp:include page="menu.jsp" /> </div> <div class="row"> <div class="col-2"></div> <div class="col-8"> <h2>Update Employee</h2> <form id="update-employee" action="update-employee" method="post"> <div class="mb-3 mt-3"> <label for="name">Name</label> <input type="text" class="form-control" id="name" placeholder="Name" name="name" value="${employee.name}" required /> <input type="hidden" name="id" value="${employee.id}" /> </div> <div class="mb-3"> <label for="department">Department</label> <input type="text" class="form-control" id="department" placeholder="Enter Department" name="department" value="${employee.department}" required /> </div> <div class="mb-3 mt-3"> <label for="designation">Designation</label> <input type="text" class="form-control" id="designation" placeholder="Enter Designation" name="designation" value="${employee.designation}" required /> </div> <div class="mb-3"> <label for="salary">Salary</label> <input type="number" class="form-control" id="salary" placeholder="Enter Salary" name="salary" value=${employee.salary } required /> </div> <button type="submit" class="btn btn-primary">Update Employee</button> </form> </div> <div class="col-2"></div> </div> <div class="row"> <jsp:include page="footer.jsp" /> </div> </body> </html> |
menu.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container-fluid"> <a class="navbar-brand" href="#">Employee Management</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavDarkDropdown"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"><a class="nav-link active" aria-current="page" href="/EmployeeManagement">Home</a></li> <li class="nav-item"><a class="nav-link active" aria-current="page" href="/EmployeeManagement">Show</a></li> <li class="nav-item"><a class="nav-link active" aria-current="page" href="AddEmployee.jsp">Add</a></li> </ul> </div> </div> </nav> |
footer.jsp
1 2 3 4 5 6 7 8 9 10 11 12 | <div class="container"> <footer class="py-3 my-4"> <ul class="nav justify-content-center border-bottom pb-3 mb-3"> <li class="nav-item"><a href="#" class="nav-link px-2 text-muted">Home</a></li> <li class="nav-item"><a href="#" class="nav-link px-2 text-muted">Features</a></li> <li class="nav-item"><a href="#" class="nav-link px-2 text-muted">About</a></li> <li class="nav-item"><a href="#" class="nav-link px-2 text-muted">Contact</a></li> <li class="nav-item"><a href="#" class="nav-link px-2 text-muted">FAQ's</a></li> </ul> <p class="text-center text-muted">© 2021 Company, Inc</p> </footer> </div> |
