aspose file tools*
The moose likes JSP and the fly likes Paging in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Paging in jsp" Watch "Paging in jsp" New topic
Author

Paging in jsp

Munna Singh
Greenhorn

Joined: Mar 17, 2009
Posts: 1
Hi, Everyone

Im facing problem in paging using jsp & oracle ,in this code only 1 to 10 values

displying,when im clicking on next its displying nothing.Means not taking next

values.

Code-:
<%@ page language="java" import="java.sql.*;"%>
<html>
<head>
</head>
<body>
<br><br><br>
<%
int offset=0;
int ofset = 0;
if(request.getParameter("offset")!=null) {
offset= Integer.parseInt(request.getParameter("offset").toString());
}
int total_count = 0;
int total_page = 0;
int per_page =10;
String name="";
if(offset>1){
ofset = offset*per_page-per_page;
}
System.out.println("MySQL Connect Example.");
Connection conn = null;
String color = "#F9EBB3";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbcracle:thin:@localhost:1521:ORCL",
"newton" , "newton");
Statement stcount = conn.createStatement();
String strQuerycount = "select count(*) from userdetail ";
ResultSet rscount = stcount.executeQuery(strQuerycount);
if(rscount.next())
{
total_count=rscount.getInt(1);
}
if(total_count%per_page==0)
{
total_page = total_count/per_page;
}
else
{
total_page = total_count/per_page+1;
}
//out.println(total_count);
//out.println(total_page);
//out.println(ofset);
Statement st = conn.createStatement();
String strQuery = "select * from userdetail where rownum>="+ofset+" and rownum<="+per_page;
// out.println(strQuery);
ResultSet rs2=null;
ResultSet rs = st.executeQuery(strQuery);
// out.println("offset " + offset);
int count=0;
if(offset>1)
count=offset*per_page-per_page;
%>
<br><br><br>
<table width="400px" align="center" style="border:1px solid #000000;" >
<tr><td colspan=8 align="center" style="background-color:ffeeff"><b>Transaction Report</b></td>
</tr>
<tr style="background-color:efefef;">
<td><b>SNo</b></td>
<td><b>Name</b></td>
<td><b>Address</b></td>
</tr>
<%
while(rs.next()){
if((count%2)==0){
color = "#eeffee";
}
else{
color = "#F9EBB3";
}
count++;
%>
<tr style="background-color:<%=color%>;">
<td><%=count%></td>
<td><%=rs.getString(1)%> </td>
<td><%=rs.getString(2)%> </td>
</tr>
<%
}
%>
</table>
<br><br>
<table width="100px" align="center" border=0><tr>
<%
if(count==0)
{
%>
<tr style="background-color:<%=color%>;">
<td colspan=8 align="center">No Record</td>
</tr>
<%
}
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
if(offset>1){
int previous = offset-1;
%>
<td>
<a href="orderbooking.jsp?offset=<%=previous%>">Previous</a></td>
<%
}
if(total_page>0)
{
for(int i=1;i<=total_page;i++)
{
if(request.getParameter("offset")==null)
{
offset=1;
}
if(i==offset)
{
%>
<td>
<%=i%></td>
<%
}
else{
%>
<td>
<a href="orderbooking.jsp?offset=<%=i%>"><%=i%></a></td>
<%
}
}
}
if(offset<total_page)
{
int next = offset+1;
%>
<td>
<a href="orderbooking.jsp?offset=<%=next%>">Next</a></td>
<%
}
%>
</tr>
</table>
</body>
</html>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

I didn't look at your complete code, but your code looks very bad. Why are you doing all this business logic inside your JSP. Anyways it seems that you have used a column rownum in your database just for pagination. That is not required. Generally you can use a SQL query which limits the number of records and also decides the first record. For example in mySql I could use

select * from users limit 10, 20;

This statement will select 20 records starting from the 10th record...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Anurag Blore
Ranch Hand

Joined: Jan 15, 2003
Posts: 74
Munna,

In general if you are trying to do pagination then you can use either database related pagination or java related.

For java I can say if the data is less then you can hold all the data in a collection and add the simple next previous button to navigate(you need to call the java method everytime you click the previous/next button and the page will refresh using the data)

2. Second option is you can use the display tag library which is easy to use again if data is not too huge.
display tag

3. If you are using struts then you can use logic iterator tag which has capability to iterate for specified position.
Struts logic Iterator


Thanks,
Anurag
SCJP 1.2 & 1.5, PMP
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Although the UI is JSF targeted, you may get lot of useful ideas out of the business and data logic found here when the data is huge: http://balusc.blogspot.com/2008/10/effective-datatable-paging-and-sorting.html
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
[Thread hijack removed. Please start your own topics for your own questions.]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Paging in jsp