Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pagination in jsp,java
12-19-2011, 05:32 PM
Post: #1
Star pagination in jsp,java
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
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Tongue Students management java project download rajasri 25 2,290 05-15-2012 12:45 PM
Last Post: itischinmay
  shopping cart in jsp&java admin 6 1,072 04-13-2012 08:51 PM
Last Post: sushantkop
Sad java.sql.SQLException: General error solution rajasri 3 1,090 02-07-2012 03:05 PM
Last Post: shahbaz07dbit
  pagination in jsp admin 1 2,734 01-12-2012 05:14 PM
Last Post: pawan
Sad create a file from form input in java,jsp rajasri 0 258 12-19-2011 06:32 PM
Last Post: rajasri
Wink Reading Html code of all the url using java rajasri 0 186 12-19-2011 06:17 PM
Last Post: rajasri
Music How to extract war file in java rajasri 0 2,887 12-14-2011 12:07 PM
Last Post: rajasri
Thumbs Up Solution for java.lang.NoClassDefFoundError: javax/transaction/Synchronization rajasri 0 1,013 12-14-2011 12:00 PM
Last Post: rajasri
Video How to use batch update in java and jsp rajasri 0 471 12-12-2011 11:38 PM
Last Post: rajasri
Bug Print current time using java in jsp page rajasri 0 619 12-12-2011 11:20 PM
Last Post: rajasri

Forum Jump:


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