Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Paging in jsp

 
Munna Singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Anurag Blore
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Thread hijack removed. Please start your own topics for your own questions.]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic