Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ramya
pagination
5 Answer(s)      2 years and 2 months ago
Posted in : JSP-Servlet


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....


Advertisement
View Answers

July 27, 2012 at 12:24 PM


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>


July 27, 2012 at 12:24 PM


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:

http://www.roseindia.net/jsp/paging.shtml



July 27, 2012 at 12:30 PM


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;
    }
}


July 27, 2012 at 12:30 PM


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>


August 1, 2012 at 12:09 PM


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+"'";



Related Tutorials/Questions & Answers:
Pagination
Pagination  How to apply pagination in JSP. Please help
Pagination
Pagination  How to implement pagination in struts using hibernate
Advertisements
Pagination
Pagination  How to create pagination in jsp with EJB using MS SQL
pagination
pagination  How to set pagination using java script to display data
Pagination
Pagination  I want to write one where condition in sql query in pagination concept
Pagination
Pagination  Is their plug n play approach for creating pagination? 1) Server side as well as client side
pagination
pagination   Simple way for pagination in jsp using java script to display data without send database
Pagination
Pagination  I want to know how to use pagination in jsp. I have a list object from that i want to display 3 questions per page can u help me
pagination
pagination  I need to give pagination with where condition (query="select * from qtn where qid='"+replyQuesionId+"'limit "+iPageNo+","+showRows+"" ) like this I want, without where condition it is working but with condition
pagination
pagination  I need to give pagination with where condition (query.......   Here is a simple jsp pagination code where we have used mysql database...()) { totalRows=rs2.getInt("cnt"); } %> <html> <h3>Pagination of JSP
Pagination
=rs2.getInt("cnt"); } %> Pagination of JSP page Terr_Code Terr_Name
pagination
code of pagination. In the given code,we have taken the database table student...;Pagination of JSP page</h3> <body> <form> <input type="hidden
Pagination
Pagination  pagination in jsp   Hi Friend, Try the following code: <%@ page language="java" %> <%@ page import="java.sql.*" %>...()) { totalRows=rs2.getInt("cnt"); } %> <html> <h3>Pagination of JSP
pagination
pagination  how can i start pagination in jsp?   Hi Friend, Try the following code: <%@ page language="java" %> <%@ page import...;Pagination of JSP page</h3> <body> <form> <input type="hidden
Pagination in java
Pagination in java  How to handle pagination when there are records like say in millions in java? Or do we handle this using SQL? I as asked... far good performance then Java pagination. Thanks
jsp pagination
jsp pagination  I want to implement pagination on jsp page Each jsp page having some radio buttons, on click of next previous page selected radio buttons are reset. I want to maintain state of selected radio buttons on previous
pagination in jsf
pagination in jsf  Hi , i am implemeting an online exam system , i have placed my questions as an arraylist in my jsp file within tag , but now i want to paginate these questions based on respective questions by clicking
JSP Pagination
to be shown, I want to implement Pagination.. Can someone show me a sample code which I could possibly use??   JSP Pagination pagination.jsp: <%@ page..."); } %> <html><h3>Pagination of JSP page</h3> <body><
Hibernate Pagination
In this tutorial we will discuss concept of pagination in hibernate
jsf pagination
of pagination. In the given code,we have taken the database table student(rollNo,name,marks...()) { totalRows=rs2.getInt("cnt"); } %> <html> <h3>Pagination of JSP
Pagination in jsp - JSP-Servlet
Pagination in jsp  I need an example of pagination in JSP using display tag
Two Pagination in one page
Two Pagination in one page  hai friends any one help me. how do u make two pagination script in same page i'm used some ajax coding one pagination script is working but another pagination is not working please help me urgent
Pagination example with html code
Pagination example with html code  Hi, could u please provide pagination code with clear cut explanation. Thanks in advance
pagination in hibernate with jsp
pagination in hibernate with jsp  Hi, plz give me example on pagination .   Hi Friend, Visit Here Thanks
pagination - JSP-Servlet
pagination  Hi, could u tell me how i will handle pagination... friend, Simple pagination in jsp Simple pagination in JSP... Simple pagination in jsp Displaying Records
pagination in jsp - JDBC
pagination in jsp  Hi, I have millions of records in my oracle d... in advance...   Hi friend, This is simple code of pagination. Simple pagination in jsp
Pagination without using database in php
Pagination without using database in php  How can I show multiple images in multiple rows with pagination in php
pagination+dynamic pagesize+hibernate - Struts
pagination+dynamic pagesize+hibernate  pagination using hibernate with dynamic page size plzz heilp me
Advertisements
 

 

 

DMCA.com