----------------------------------- ConnectionFactory.java -------------------------------------------
package com.ramsiscode.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
//static reference to itself
private static ConnectionFactory instance =
new ConnectionFactory();
String url = "jdbc:mysql://localhost:3306/exampledb";
String user = "asdfg";
String password = "cctns@7777";
String driverClass = "com.mysql.jdbc.Driver";
//private constructor
private ConnectionFactory() {
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static ConnectionFactory getInstance() {
return instance;
}
public Connection getConnection() throws SQLException,
ClassNotFoundException {
Connection connection =
DriverManager.getConnection(url, user, password);
return connection;
}
}
----------------------------------- EmployeeServlet.java -------------------------------------------
package com.ramsiscode.servlets;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ramsiscode.dao.EmployeeDAO;
import com.ramsiscode.to.Employee;
/**
* Servlet implementation class EmployeeServlet
*/
public class EmployeeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public EmployeeServlet() {
super();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int page = 1;
int recordsPerPage = 8;
if(request.getParameter("page") != null)
page = Integer.parseInt(request.getParameter("page"));
EmployeeDAO dao = new EmployeeDAO();
List<Employee> list = dao.viewAllEmployees((page-1)*recordsPerPage,
recordsPerPage);
int noOfRecords = dao.getNoOfRecords();
int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
request.setAttribute("employeeList", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("displayEmployee.jsp");
view.forward(request, response);
}
}
----------------------------------- EmployeeDAO.java -------------------------------------------
package com.ramsiscode.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.ramsiscode.db.ConnectionFactory;
import com.ramsiscode.to.Employee;
public class EmployeeDAO {
Connection connection;
Statement stmt;
private int noOfRecords;
public EmployeeDAO() { }
private static Connection getConnection()
throws SQLException,
ClassNotFoundException
{
Connection con = ConnectionFactory.
getInstance().getConnection();
return con;
}
public List<Employee> viewAllEmployees(
int offset,
int noOfRecords)
{
String query = "select SQL_CALC_FOUND_ROWS * from employee limit "
+ offset + ", " + noOfRecords;
List<Employee> list = new ArrayList<Employee>();
Employee employee = null;
try {
connection = getConnection();
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
employee = new Employee();
employee.setEmployeeId(rs.getInt("emp_id"));
employee.setEmployeeName(rs.getString("emp_name"));
employee.setSalary(rs.getDouble("salary"));
employee.setDeptName(rs.getString("dept_name"));
list.add(employee);
}
rs.close();
rs = stmt.executeQuery("SELECT FOUND_ROWS()");
if(rs.next())
this.noOfRecords = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally
{
try {
if(stmt != null)
stmt.close();
if(connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public int getNoOfRecords() {
return noOfRecords;
}
}
--------------------------------------- Employee.java ---------------------------------------
package com.ramsiscode.to;
public class Employee {
private int employeeId;
private String employeeName;
private double salary;
private String deptName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
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;
}
}
---------------------------------- displayEmployee.jsp -----------------------------------------
<%@ 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 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=ISO-8859-1">
<title>Employees</title>
</head>
<body>
<table border="1" cellpadding="3" cellspacing="3">
<tr>
<th>Emp ID</th>
<th>Emp Name</th>
<th>Salary</th>
<th>Dept Name</th>
</tr>
<c:forEach var="employee" items="${employeeList}">
<tr>
<td>${employee.employeeId}</td>
<td>${employee.employeeName}</td>
<td>${employee.salary}</td>
<td>${employee.deptName}</td>
</tr>
</c:forEach>
</table>
<%--For displaying Previous link except for the 1st page --%>
<c:if test="${currentPage != 1}">
<td><a href="employee.do?page=${currentPage - 1}">Previous</a></td>
</c:if>
<%--For displaying Page numbers.
The when condition does not display a link for the current page--%>
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<c:forEach begin="1" end="${noOfPages}" var="i">
<c:choose>
<c:when test="${currentPage eq i}">
<td>${i}</td>
</c:when>
<c:otherwise>
<td><a href="employee.do?page=${i}">${i}</a></td>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
</table>
<%--For displaying Next link --%>
<c:if test="${currentPage lt noOfPages}">
<td><a href="employee.do?page=${currentPage + 1}">Next</a></td>
</c:if>
</body>
</html>
------------------------------------ web.xml -----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JSPPagination</display-name>
<welcome-file-list>
<welcome-file>employee.do</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>com.ramsiscode.servlets.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/employee.do</url-pattern>
</servlet-mapping>
</web-app>
No comments:
Post a Comment