Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pagination in jsp
12-26-2011, 03:19 PM (This post was last modified: 12-26-2011 03:20 PM by admin.)
Post: #1
pagination in jsp
Hai all today we are going to learn how to create a pagination in jsp.
Understanding Pagination is first of all important,Retrieving and showing 50 values in a page is not a problem if it is more than 100 or 200 values then page which is showing is too long and time to load so know now,Actually pagination is when you want to retrieve all values from database but show only limited value in first page and if users wants he clicks on second page to view the remaining items.
Lets create the table
"create table chart(name varchar(20));"
I use mysql if you use anyother db change the coding connection lines according to that.

Here is the jsp code

<%@page import="java.sql.*,java.io.*,java.math.*,java.lang.*"%>
Tutorial from In order to view links, you must have to reply to this thread. give your comments
<h1>Simple pagination in jsp from In order to view links, you must have to reply to this thread.</h1>
<%
int total_entries=0;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/s2sgateway","root","root");
Statement st=con.createStatement();
Statement st1=con.createStatement();
ResultSet rs=st.executeQuery("SELECT count(*) from chart");
rs.next();
int count=rs.getInt(1);
total_entries =count;

int page_number=0;

int total_pages=0;
int i;
int entries_per_page = 2;
if(request.getParameter("page_number")!=null)
{
page_number = Integer.parseInt(request.getParameter("page_number"));
} else {
page_number = 1;
}
total_pages = (total_entries / entries_per_page);
int offset = (page_number - 1) * entries_per_page;
ResultSet rs1=st1.executeQuery("SELECT * FROM chart ORDER BY name ASC LIMIT
"+offset+","+entries_per_page);
while(rs1.next())
{
out.println(rs1.getString(1));
// Display the data anything from database

}
for(i = 1; i <= total_pages; i++)
{ if(i == page_number)
{
// This is the only page. so no link
out.println("pagenumber"+i);
}
else {
// This is not the only page. Make it a link.

%>
<a href="pagination.jsp?page_number=<%=i%>"><%=i%></a>
<%
}
}
%>

Here entries_per_page is the no of entries you need on the page.It display links when database values more than 3.Otherwise display values in one page.You can modify this code to suite with hibernate and struts as well.Any doubt ask us happy coding
Find all posts by this user
Add Thank You Quote this message in a reply
01-12-2012, 09:44 PM
Post: #2
RE: pagination in jsp
(12-26-2011 03:19 PM)admin Wrote: In order to view links, you must have to reply to this thread.Hai all today we are going to learn how to create a pagination in jsp.
Understanding Pagination is first of all important,Retrieving and showing 50 values in a page is not a problem if it is more than 100 or 200 values then page which is showing is too long and time to load so know now,Actually pagination is when you want to retrieve all values from database but show only limited value in first page and if users wants he clicks on second page to view the remaining items.
Lets create the table
"create table chart(name varchar(20));"
I use mysql if you use anyother db change the coding connection lines according to that.

Here is the jsp code

<%@page import="java.sql.*,java.io.*,java.math.*,java.lang.*"%>
Tutorial from In order to view links, you must have to reply to this thread. give your comments
<h1>Simple pagination in jsp from In order to view links, you must have to reply to this thread.</h1>
<%
int total_entries=0;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/s2sgateway","root","root");
Statement st=con.createStatement();
Statement st1=con.createStatement();
ResultSet rs=st.executeQuery("SELECT count(*) from chart");
rs.next();
int count=rs.getInt(1);
total_entries =count;

int page_number=0;

int total_pages=0;
int i;
int entries_per_page = 2;
if(request.getParameter("page_number")!=null)
{
page_number = Integer.parseInt(request.getParameter("page_number"));
} else {
page_number = 1;
}
total_pages = (total_entries / entries_per_page);
int offset = (page_number - 1) * entries_per_page;
ResultSet rs1=st1.executeQuery("SELECT * FROM chart ORDER BY name ASC LIMIT
"+offset+","+entries_per_page);
while(rs1.next())
{
out.println(rs1.getString(1));
// Display the data anything from database

}
for(i = 1; i <= total_pages; i++)
{ if(i == page_number)
{
// This is the only page. so no link
out.println("pagenumber"+i);
}
else {
// This is not the only page. Make it a link.

%>
<a href="pagination.jsp?page_number=<%=i%>"><%=i%></a>
<%
}
}
%>

Here entries_per_page is the no of entries you need on the page.It display links when database values more than 3.Otherwise display values in one page.You can modify this code to suite with hibernate and struts as well.Any doubt ask us happy coding
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies Views: Last Post
Star pagination in jsp,java rajasri 0 93 12-19-2011 10:02 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)


WE will share important jobs and updates on facebook so like us to get updates