Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JSP Pagination Example


We can create pagination example in JSP easily. It is required if you have to display many records. Displaying many records in a single page may take time, so it is better to break the page into parts. To do so, we create pagination application.

In this pagination example, we are using MySQL database to fetch records.

We have created “emp” table in “test” database. The emp table has three fields: id, name and salary. Either create table and insert records manually or import our sql file.

index.jsp

  1. <a href=“view.jsp?page=1”>View Employees</a>

view.jsp

  1. <%@ page import=“java.util.*,com.Kreationnext.dao.*,com.Kreationnext.beans.*” %>
  2. <%
  3. String spageid=request.getParameter(“page”);
  4. int pageid=Integer.parseInt(spageid);
  5. int total=5;
  6. if(pageid==1){}
  7. else{
  8.     pageidpageid=pageid-1;
  9.     pageidpageid=pageid*total+1;
  10. }
  11. List<Emp> list=EmpDao.getRecords(pageid,total);
  12. out.print(“<h1>Page No: “+spageid+”</h1>“);
  13. out.print(“<table border=‘1’ cellpadding=‘4’ width=‘60%’>“);
  14. out.print(“<tr><th>Id</th><th>Name</th><th>Salary</th>“);
  15. for(Emp e:list){
  16.     out.print(“<tr><td>“+e.getId()+”</td><td>“+e.getName()+”</td><td>“+e.getSalary()+”</td></tr>“);
  17. }
  18. out.print(“</table>“);
  19. %>
  20. <a href=“view.jsp?page=1”>1</a>
  21. <a href=“view.jsp?page=2”>2</a>
  22. <a href=“view.jsp?page=3”>3</a>

Emp.java

  1. package com.Kreationnext.beans;
  2. public class Emp {
  3. private int id;
  4. private String name;
  5. private float salary;
  6. //getters and setters
  7. }

EmpDao.java

  1. package com.Kreationnext.dao;
  2. import com.Kreationnext.beans.*;
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class EmpDao {
  7.     public static Connection getConnection(){
  8.         Connection con=null;
  9.         try{
  10.             Class.forName(“com.mysql.jdbc.Driver”);
  11.             con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,“”,“”);
  12.         }catch(Exception e){System.out.println(e);}
  13.         return con;
  14.     }
  15.     public static List<Emp> getRecords(int start,int total){
  16.         List<Emp> list=new ArrayList<Emp>();
  17.         try{
  18.             Connection con=getConnection();
  19.             PreparedStatement ps=con.prepareStatement(“select * from emp limit “+(start-1)+“,”+total);
  20.             ResultSet rs=ps.executeQuery();
  21.             while(rs.next()){
  22.                 Emp e=new Emp();
  23.                 e.setId(rs.getInt(1));
  24.                 e.setName(rs.getString(2));
  25.                 e.setSalary(rs.getFloat(3));
  26.                 list.add(e);
  27.             }
  28.             con.close();
  29.         }catch(Exception e){System.out.println(e);}
  30.         return list;
  31.     }
  32. }

Download SQL File

Download SQL File

Download mysql-connector.jar file

Download mysql-connector.jar

Download Project

Download this example (Developed in Eclipse)

Output

pagination1

pagination2

pagination3

pagination4

Comments

comments