display 10 records per page in jsp

display 10 records per page in jsp

View Answers

October 8, 2009 at 1:59 PM

Hi Friend,

Try the following code:

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>

October 8, 2009 at 2:00 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>
<%
try{
if(ps1!=null){
ps1.close();
}
if(rs1!=null){
rs1.close();
}

if(ps2!=null){
ps2.close();
}
if(rs2!=null){
rs2.close();
}

if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>

In the above code,we have taken the database table student(rollNo,name,marks,grade).

Thanks

September 1, 2012 at 3:28 PM

int totalRows=nullIntconvert(request.getParameter("totalRows")); int totalPages=nullIntconvert(request.getParameter("totalPages")); int iPageNo=nullIntconvert(request.getParameter("iPageNo")); int cPageNo=nullIntconvert(request.getParameter("cPageNo"));

please tell me from where these lines have come from i.e request.getParameter("totalPages") and so on.









Related Tutorials/Questions & Answers:
display 10 records per page in jsp - JSP-Servlet
display 10 records per page in jsp  hello, can i get a code that displays 10 records per page from a table student and when i click on the next button it displays the next 10 pages and so on. Im using jsp and mysql database
code to display 20 record per page and display page 1,2,3,4 as recoerd increase - JSP-Servlet
code to display 20 record per page and display page 1,2,3,4 as recoerd increase  Here is my code i wnat to diplay 15 to 20 records per page please... and previous button or display page no 1,2,3.. this is my code please any one
Advertisements
I want to display 10,000 records in a jsp page.Can I display page by page?
I want to display 10,000 records in a jsp page.Can I display page by page? ... print page by page or have say 900 records per page in a jsp? Should I have to write a for loop in the output.jsp page? Please help me with this requirement
How to display mysql database records as per clock timing?
How to display mysql database records as per clock timing?  I want to display database records as per clock timing
large nuber of records display to jsp - Development process
to a jsp page using pagination. I want to display 1oo records per page, remaining... that will display the records to jsp page using pagination.Here we have specified 10 records per page.Try the following code: pagination.jsp
display records with images problem - JSP-Servlet
display records with images problem  hello, i am developing HR application whereby i want to display employee records and their pictures on web... with a unique id. Also i wrote i jsp that link to servlet in order to display
retrieving newly added records from mssql database and display in a jsp
these 10 records from the jsp and retrieve the next recently added 10 records...retrieving newly added records from mssql database and display in a jsp  Here is my requirement, I have to retrieve recently added 10 records
Display Records in Tree Format - JSP-Servlet
Display Records in Tree Format   hi i want to displays data in the tree format with root nodes, child nodes and leaf nodes in a jsp file... we have to pass values of root node subnodes and the leaf node to next jsp
please give me solution how to display next page after 20 records ? - JSP-Servlet
please give me solution how to display next page after 20 records ?  Java Servlet Paging control example  here i have attached one example..." } } } 1 2 3 4 5 6 7 8 9 10
Upload and display image and text records using JSP and Oracle
Upload and display image and text records using JSP and Oracle  Hi all, I'm using JSP and Oracle 10g. I could retrieve and display records from database. But i couldnot retrieve and display image. I want to store
display results on same jsp page
display results on same jsp page  is there any method to display search results from database on the same jsp page where i give the inputs using struts 1.2
display results on same jsp page
display results on same jsp page  is there any method to display search results from database on the same jsp page where i give the inputs using struts 1.2
display results on same jsp page
display results on same jsp page  is there any method to display search results from database on the same jsp page where i give the inputs using struts 1.2
display 1000 of records - Struts
display 1000 of records  how to display 1000 of records using next... that we can display any number of records. Implement a sample   We have pagination concept in Java. Using that we can display any number of records
search an element in arraylist and display in jsp page
search an element in arraylist and display in jsp page  Hello Can anyone help how to take input from user from a jsp page to search for a substing in arraylist in java class and display the list in jsp page Thanks
How to Display jrxml file on JSP page???
How to Display jrxml file on JSP page???  I made jrxml file using jasper report but how to display on jsp page?? i tried lot but still i didnt find out solution.so pls help me as soon as possible
display checked ckeckbox in same jsp page
display checked ckeckbox in same jsp page  how to display checked ckeckbox in same jsp page?   Hi, Here is the html code to display check... jsp page. (for example, as it happens in VB that after clicking checkbox
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me   displaying List of records from database in a jsp using ajax, onclick it should display the results
How to display a record containing 30 fields in jsp without scrolling the page horizontally?
How to display a record containing 30 fields in jsp without scrolling the page horizontally?  I want to display records containing 30 fields in a table row.Is it possible in jsp without scrolling the page horizontally
Request header display in a jsp page
Request header display in a jsp page  ... to display request header information in a jsp page. When user request... to a jsp. In this example, request_header_jsp.jsp is used to display header
how to display records from database
how to display records from database  I want to display records from database in tables, the database is having 2000 records and i want to display 20 records at a time and to use next and previous link buttons to show
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
Data displaying with limited records in jsp
Data displaying with limited records in jsp  How to display table with limited 10 records , after clicking next button another 10 records from database upto last record please help me
How to display Jfreechart from servlet in jsp web page at specified location
How to display Jfreechart from servlet in jsp web page at specified... to display the chart in web page. I generated the chart using Jfreechart... in jsp web page . Thank you very much Sir
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads... = DEDCHGG_PWDP and goes on... I have to create a jsp page to show these data
how to display each arraylist value on new page in jsp
how to display each arraylist value on new page in jsp  hi!!!! i want to display each arraylist value on new page in jsp????? and also want to access...://www.roseindia.net/jsp/servlet-jsp-data-list.shtml
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
Retrieving newly inserted records and displaying in jsp forever
those records in the jsp. If the session-duration exceeds 10 minutes for any...Retrieving newly inserted records and displaying in jsp forever  Sir, here is my requirement, First i have to retrieve newly added 10 records from
how to use dropdown list in JSP and display value in same page
how to use dropdown list in JSP and display value in same page  I... in the same page Value selected = C How can we do this in a JSP page... one option the value must get displayed below it in the same page
Display image on JSP page using XML
Display image on JSP page using XML  ... can display a image on JSP page by using XML.  This example will examine how to parse and expose XML information using the JAXP with a JSP page
pre Page load display an image loading - JSP-Servlet
pre Page load display an image loading  hi, We have requirement...;Hi Friend, Do you want to display image before loading page? Please clarify this. Thanks  Yes i have to display an loading image while page
How to display date in JSP?
How to display date in JSP?  How to create a simple JSP program which displays date on the page? Thanks
printing records from jsp - JSP-Servlet
printing records from jsp  Hi Plz tell me how to printing out pages from jsps with page numbers Thank you  Hi friend, For paging in Jsp visit to : http://www.roseindia.net/jsp/paging.shtml http
how can manages records in jsp
how can manages records in jsp  how can sho only five record at a time and next 5 on click next button and so on in jsp
How to check no records - JSP-Servlet
project, in that how i have 2 check no records. I saw an answer... to check and display your cart is empty.... PLs help me......... Reply... not empty //display something else //false resultset empty //display something
How to display date in JSP?
How to display date in JSP?  How to create a simple JSP program which displays date on the page? Thanks   Hi, You should import...() %> Check complete tutorial at How to Create JSP Page? Thanks
display image using jsp
display image using jsp  display image using jsp and phonegap on emulator of eclipse   Here is a simple jsp code that displays an image on browser. <%@ page import="java.io.*" %> <%@page contentType="image/gif
display image on jsp
is downloading on jsp. show i can open it. but i want to display that image on jsp...display image on jsp  how to display image from database which path is coming in string. i want to display image and its discription and price
NEXT and PREVIOUS Records - JSP-Servlet
NEXT and PREVIOUS Records  Dear Sir, In my Project i have next and previous buttons at bottom of my page which is working fine.But as per our requirement next,previous buttons should be placed at top of the page as well
Display Java in JSP
Display Java in JSP  please i need urgent help. How can i display Java Applet created in netbeans in JSP?. the Application involves MYSQL Database connection. Thanks
image display - JSP-Servlet
image display  how to display image that store in mysql database... column have BLOB datatype....  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/retrieve-image.shtml Thanks
how to display the email message in jsp
how to display the email message in jsp  hi every one .. i am new from this industry please help me to display the email message in jsp page please send me sample code
Jsp Image Display
Jsp Image Display  Hi,i need to display image in a Box like... to save it in a database....Kindly post JSP codes..Its Urgent, I m very thankful t... on the browser. 1)page.jsp: <%@ page language="java" %> <HTML> <HEAD>
dynamic display - JSP-Servlet
javax.servlet.http.*; public class DataServlet extends HttpServlet{ String page="/jsp...dynamic display  hi, i want to display dynamic values in drop drown box in a jsp page.these values are in the form of arraylist's object which
displaying both image and records problem in jsp and servlet - JSP-Servlet
displaying both image and records problem in jsp and servlet  hello... the application to display more than one employee records and their pictures. e.g. say i'm having records like: firstname, lastname, empid, department, salary, job_title
display an image on my page
display an image on my page  How can I display an image on my page
image display in jsp - Java Beginners
image display in jsp  i uploaded the image and how can i print that image in next jsp page....  Hi friend, read for more information, http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?
display page for a resultSet() in jsp?  this is count record code <%@ page language="java" import="java.sql.*" %> <html> <head>...;%@ page language="java" import="java.sql.*" %> <html> <head> <

Ads