I need to give pagination with where condition (query="select * from qtn where qid='"+replyQuesionId+"'"; ) like this without where condition it is working but with condition it is not entering into next page....
Here is a simple jsp pagination code where we have used mysql database table student(rollNo,name,marks,grade) to display the data in proper way.
pagination.jsp:
<%@ page language="java" %> <%@ page import="java.sql.*" %> <%! public int nullIntconvert(String str){ int num=0; if(str==null) { str="0"; } else if((str.trim()).equals("null")) { str="0"; } else if(str.equals("")) { str="0"; } try{ num=Integer.parseInt(str); } catch(Exception e) { } return num; } %> <% Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); ResultSet rs1 = null; ResultSet rs2 = null; PreparedStatement ps1=null; PreparedStatement ps2=null; int showRows=10; int totalRecords=10; int totalRows=nullIntconvert(request.getParameter("totalRows")); int totalPages=nullIntconvert(request.getParameter("totalPages")); int iPageNo=nullIntconvert(request.getParameter("iPageNo")); int cPageNo=nullIntconvert(request.getParameter("cPageNo")); int startResult=0; int endResult=0; if(iPageNo==0) { iPageNo=0; } else{ iPageNo=Math.abs((iPageNo-1)*showRows); } String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit "+iPageNo+","+showRows+""; ps1=conn.prepareStatement(query1); rs1=ps1.executeQuery(); String query2="SELECT FOUND_ROWS() as cnt"; ps2=conn.prepareStatement(query2); rs2=ps2.executeQuery(); if(rs2.next()) { totalRows=rs2.getInt("cnt"); } %> <html> <h3>Pagination of JSP page</h3> <body> <form> <input type="hidden" name="iPageNo" value="<%=iPageNo%>"> <input type="hidden" name="cPageNo" value="<%=cPageNo%>"> <input type="hidden" name="showRows" value="<%=showRows%>"> <table width="100%" cellpadding="0" cellspacing="0" border="1" > <tr> <td>Roll No</td> <td>Name</td> <td>Marks</td> <td>Grade</td> </tr> <% while(rs1.next()){ %> <tr> <td><%=rs1.getInt("rollNo")%></td> <td><%=rs1.getString("name")%></td> <td><%=rs1.getInt("marks")%></td> <td><%=rs1.getString("grade")%></td> </tr> <% } %> <% try{ if(totalRows<(iPageNo+showRows)) { endResult=totalRows; } else{ endResult=(iPageNo+showRows); } startResult=(iPageNo+1); totalPages=((int)(Math.ceil((double)totalRows/showRows))); } catch(Exception e){ e.printStackTrace(); } %> <tr> <td colspan="3"> <div>
continue..
<% int i=0; int cPage=0; if(totalRows!=0){ cPage=((int)(Math.ceil((double)endResult/(totalRecords*showRows)))); int prePageNo=(cPage*totalRecords)-((totalRecords-1)+totalRecords); if((cPage*totalRecords)-(totalRecords)>0){ %> <a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a> <% } for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){ if(i==((iPageNo/showRows)+1)){%> <a href="pagination.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a> <% } else if(i<=totalPages){ %> <a href="pagination.jsp?iPageNo=<%=i%>"><%=i%></a> <% } } if(totalPages>totalRecords && i<totalPages){ %> <a href="pagination.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> Next</a> <% } } %> <b>Rows <%=startResult%> - <%=endResult%> Total Rows <%=totalRows%> </b> </div></td></tr></table></form></body></html>
For more information, visit the following link:
Here is another code of servlet for pagination. In the given coe, we have used ArrayList to store the dta and display it as paging.
1)PaginationServlet.java:
import java.io.*; import java.util.*; import form.Student; import javax.servlet.*; import javax.servlet.http.*; public class PaginationServlet extends HttpServlet { int offset; int length; List list; protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { int maxEntriesPerPage = 2; int page = 1; String pageNumberValue = request.getParameter("pageNumber"); if (pageNumberValue != null) { try { page = Integer.parseInt(pageNumberValue); System.out.println("Page Number:" + page); } catch (NumberFormatException e) { e.printStackTrace(); } } int offset = maxEntriesPerPage * (page - 1); TestList(offset, maxEntriesPerPage); HttpSession httpSession = request.getSession(); httpSession.setAttribute("pages", getPages()); httpSession.setAttribute("studentDetails", getListByLength()); RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/paging.jsp"); dispatcher.forward(request, response); } public void fillList() { list = new ArrayList(); list.add(new Student("A", "Delhi", 25, "Btech")); list.add(new Student("B", "Mumbai", 26, "Mtech")); list.add(new Student("C", "Kolkata", 27, "MSC")); list.add(new Student("D", "Chennai", 28, "MBA")); list.add(new Student("E", "Hyderabad", 23, "MCA")); list.add(new Student("F", "Agra", 24, "MBBS")); list.add(new Student("G", "Shimla", 26, "BDS")); list.add(new Student("H", "Lucknow",25,"MCom")); } public void TestList(int offset, int length) { this.offset = offset; this.length = length; fillList(); } public ArrayList getListByLength() { ArrayList arrayList = new ArrayList(); int to = this.offset + this.length; if (this.offset > list.size()) this.offset = list.size(); if (to > list.size()) to = list.size(); for (int i = this.offset; i < to; i++) { arrayList.add(list.get(i)); } return arrayList; } public List getPages() { List pageNumbers = new ArrayList(); int pages = list.size() / this.length; if (list.size() % this.length != 0) { pages = pages + 1; } for (int i = 1; i <= pages; i++) { pageNumbers.add(new Integer(i)); } return pageNumbers; } }
continue..
2)Student.java
package form; import java.io.*; public class Student implements Serializable { private String name; private String address; private int age; private String degree; public Student(String name, String address, int age,String degree) { this.name = name; this.address = address; this.age = age; this.degree = degree; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } }
The above java class is a bean and should be located inside /classes/form/Student.
3)paging.jsp:
<%@page import="java.util.List"%> <%@page import="form.Student"%> <html> <h1>Pagination</h1> <% List list = (List) session.getAttribute("studentDetails"); List pageNumbers = (List) session.getAttribute("pages"); %> <table border="1"> <tr><th>Name</th><th>Address</th><th>Age</th><th>Degree</th></tr> <% for (int i = 0; i < list.size(); i++) { Student st = (Student) list.get(i); %> <tr> <td><%=st.getName()%></td> <td><%=st.getAddress()%></td> <td><%=st.getAge()%></td> <td><%=st.getDegree()%></td></tr> <% } %> <td colspan="4" align="right"> <form method="get" action="../PaginationServlet"> <table> <tr> <% for (int i = 0; i < pageNumbers.size(); i++) { %> <td><a href="/examples/PaginationServlet?pageNumber=<%=pageNumbers.get(i)%>"><%=pageNumbers.get(i)%></a></td> <% } %> </tr> </table> </form> </td> </tr> </table> </html>
thank u for ur response... but am using 1st pagination code in that u given select * from Student there i need select * from student where id="+ss+"; like this... query="select * from qtn where qid='"+replyQuesionId+"'";