Originally posted by smitha rai:
I have an arraylist which contains data fetched from database. I want to display all these results in a jsp in a shot of 10 results at a time. The jsp contains two buttons namely, next and previous. Can any one help me out with a code or some tips. It is quite urgent for me please.......
hi smita,
i am pasting code here;i think it would solve ur problem;
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%
String b1=null;
String b2=null;
String b=null;
int i2=0;
int j2=0;
int i3=0;
int j3=5;
int count1=0;
String i1=null;
String j1=null;
String count=null;
try
{
b1= request.getParameter("Next");
b2= request.getParameter("Previous");
i1= request.getParameterValues("i1")[0];
j1= request.getParameterValues("j1")[0];
i2 =Integer.parseInt(i1);
j2 =Integer.parseInt(j1);
count=request.getParameterValues("count")[0];
count1=Integer.parseInt(count);
}
catch(NullPointerException ne) {}
%>
<form method="post" action="http://localhost:8080/examples/jsp/Next.jsp" name="frm" id="frm">
<%
ResultSet rs,rs1=null;
String name=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("Jdbc:Odbc:chaitu");
Statement stmt=con.createStatement();
count1++;
Vector v =new Vector();
Vector v1 = new Vector();
try
{
rs1 = stmt.executeQuery("select deptno,deptname from emp");
while(rs1.next())
{
v.addElement(rs1.getString("deptno"));
v1.addElement(rs1.getString("deptname"));
}
//String v1= (String) v.firstElement();
// System.out.println("firstelement="+v1);
// int k=v.lastIndexOf(v,1);
}
catch(SQLException se) {se.printStackTrace();}
catch(Exception e1){ System.out.println(e1+"guikgg");}
%>
<%
try
{ %>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFDFBF" >
<% if((b == null) && (count1 == 1))
{
System.out.println("in null part it is going");%>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i3+1%>-<%=j3 %> out of <%=v.size()%></font></td></tr></table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>
<% for(;i3<j3;i3++)
{%>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD" >
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i3)%></b></font> </td>
<td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b> <%=v1.elementAt(i3)%></b></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
<%}%>
<% if(v.size()>5)
{
%>
<tr><td align="center"> <input type="submit" name="Next" value="Next"></td></tr>
<input type="hidden" name="i1" value=<%=i3 %>>
<input type="hidden" name="j1" value=<%=j3 %>>
<input type="hidden" name="count" value=<%=count1 %>>
</table>
<%}
} %>
<%}catch(Exception e){}
%>
<%
try
{
if(b1.equals("Next"))
{
j2=j2+5;
if((j2 == v.size()) | | (j2 > v.size()))
{
int l2=i2+5;
%>
<input type="hidden" name="i1" value=<%=l2 %>>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=v.size() %> out of <%=v.size()%></font></td></tr>
</table>
<%
for(;i2<v.size();i2++)
{%>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD" bordercolor="#00FF00" border=0>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%></b></font> </td>
<td> <font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v1.elementAt(i2)%></b></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>
<%}%>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr><td align="center">
<input type="submit" name="Previous" value="Previous"></td></tr>
</table>
<%}
if(j2 < v.size())
{%>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=j2 %> out of <%=v.size()%></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD">
<% for(;i2<j2;i2++)
{%>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%> </b></font></td>
<td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b> <%=v1.elementAt(i2)%></b></font></td></tr>
<%}
%>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<input type="submit" name="Next" value="Next"></td>
<td> <input type="submit" name="Previous" value="Previous"></td>
</table>
<input type="hidden" name="i1" value=<%=i2 %>>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<%
}%>
<%}
}
catch(Exception e){}
%>
<%
try
{
if(b2.equals("Previous"))
{
i2=i2-10;
j2=j2-5;
if(j2 == 5)
{%>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=j2 %> out of <%=v.size()%></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>
<% for(;i2<j2;i2++)
{
%>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD" bordercolor="#00FF00" border=0>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%></b></font> </td>
<td> <font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v1.elementAt(i2)%></b></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bordercolor="red" border=0>
<tr><td>
</td>
</tr>
</table>
<%}
%>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center">
<input type="submit" name="Next" value="Next"></td></tr></table>
<input type="hidden" name="i1" value=<%=i2 %>>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<%
}
if((j2 < v.size()) && (j2 != 5))
{%>
<table width="150" cellspacing="0" cellpadding="0" align="center" border="0">
<tr><td width="15"> </td>
<td width="135"><font face="Arial,Helvetica,sans-serif" color="#008080"><%=i2+1 %>-<%=j2 %> out of <%=v.size()%></font></td></tr>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center" bgcolor="#C9CBAD">
<% for(;i2<j2;i2++)
{%>
<tr><td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b><%=v.elementAt(i2)%> </b></font></td>
<td><font face="Arial,Helvetica,sans-serif" color="#FF0000"><b> <%=v1.elementAt(i2)%></b></font></td></tr>
<%}
%>
</table>
<table width="100" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<input type="submit" name="Next" value="Next"></td>
<td> <input type="submit" name="Previous" value="Previous"></td>
</table>
<input type="hidden" name="i1" value=<%=i2 %>>
<input type="hidden" name="j1" value=<%=j2 %>>
<input type="hidden" name="count" value=<%=count1 %>>
<%}
}
}
catch(Exception e){}
%>
</form>
</body>
</html>