• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to split records?

 
vasudevan gopalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
For example i am having an employee table with 50 records .I
want to display 10 records of employee in each page one by one .
How to do that?
 
DAYANAND BURAMSHETTY
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,
I am sending three java files.
If any problem let me know.
Dayanand

//UserAdminServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.ejb.FinderException;

public class UserAdminServlet extends HttpServlet{
private HttpSession session = null;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void forward(HttpServletRequest req,HttpServletResponse res,String url)
throws ServletException, IOException{
res.setContentType("text/html");
RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
rd.forward(req, res);
}

public void service(HttpServletRequest req, HttpServletResponse res)throws IOException,ServletException
{

/**
* This is for.... IF have 50 records then It display 1,2,3,4,5...
*
*/
int pages=0;
/* *
* This is dummy vector only 10( 1 to 10 or 2 to 20 ..etc)
* records keep in this Vector and forward to the UserAdmin.jsp
* /
Vector dVector=null;
......
..............
/**
* All records are available in this(userVec)Vector ...everytime I take
* only 10 records from this Vector and stored in dummy Vector.
* contenetController(Write your own object for getting all records)
* is object from this I get all records.
*/
Vector userVec=contentController.getAllUserList();
/**
* Very first time call this servlet at that moment don't think
* about ORDER(Very first time It's NULL then It 1 to 10 records
* taking from userVec and stored in dummyVec and forward to the
* UserAdmin.jsp)
*/
String order=(String)req.getParameter("ORDER");
if(order==null){

dVector=new Vector();
pages=Math.round((dVector.size())/10);
for (Enumeration e = dVector.elements() ,int i=0; e.hasMoreElements(),i++ {
UserTB usertb=(UserTB)e.elementAt(i);
dVector.addElement(usertb);
if(i<10) break;
}
req.setAttribute("USERLIST", dVector);
req.setAttribute("PAGES", ""+pages);
this.forward(req,res,"/UserAdmin.jsp");
} else {
/** This block never excute very first time.
* First time display the (0-9 records) in UserAdmin.jsp and from that page
* we put index like 1,2,3...If click 2 It display the 10 - 19 records in that(UserAdmin.jsp)
* U put <a href="/UserAdminServlet?ORDER=2">2</a> It calls this servlet take the 10(10-19)
* records from userVec and keep into the dummyVector and forward to the UserAdmin.jsp
*/
int orderInt=(int)Integer.parseInt(order);
dVector=new Vector();
pages=Math.round((dVector.size())/10);
for (Enumeration e1 = dVector.elements() ,int i1=(orderInt-1)*10; e1.hasMoreElements(),i1++ {
UserTB usertb=(UserTB)e1.elementAt(i1);
dVector.addElement(usertb);
if(i1<((orderInt)*10)) break;
}
req.setAttribute("USERLIST", dVector);
req.setAttribute("PAGES", ""+pages);
this.forward(req,res,"/UserAdmin.jsp");

}

}//service
}

//UserTB.java
public class UserTB {
private String userName=null;
private String password=null;
...
pulic void setUserName(String userName){
this.userName=userName;
}
public void setPassword(String password){
this.password=password;
}
.....
public String getUserName(){
return userName;
}
public String getPassword(){
return passWord;
}
.....
}

//UserAdmin.jsp

<%@ page import="java.util.Vector"%>
<%@ page import="java.util.Enumeration"%>
<%@ page session = "true" %>
<%

userVec=(Vector)request.getAttribute("USERLIST");
int pages=(int)Integer.parseInt((String)request.getAttribute("PAGES"));
%>
<html>
<form NAME="admin" action="" METHOD="POST" >
<table width=100% border="1" cellspacing="2" cellpadding="5" >
<tr>

<th width="20%" class="adminhdr">User Name</th>
.......
</tr>

<%
for( Enumeration e=userVec.elements() ;e.hasMoreElements();i++){
UserTB usertb = null;
String userId=null;
usertb=(UserTB)e.nextElement();
userId=(String)usertb.getUserName();
....
%>
<tr>
<td width="20%" class="admin"> <%=userId%> </td>
</tr>
<%}%>
</table><br>
<%
//Here display index like 1,2,3,4.... if U click 1 it calls the servlet and the servlet take the records(1 -10) form
//servlet and forward to the this page.
for(int i=0;i<pages;i++){
%><a href="/UserAdminServlet?ORDER=<%=(i++)"><%=(i++)%></a>
}
%>
</form>
</body>
</html>
 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page import="java.sql.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>JdbcDemo</title>
<script language="JavaScript">
function show(img)
{
window.open(img,width=640,height=420)
}

</script>
</head>
<body>
<%
int i2=0;
int j2=3;
int count=0;
String i3=null;
String j3=null;
String count1=null;
String Next = null;
String Previous=null;
String id=null;

try
{
i3 = request.getParameter("i2");
Next = request.getParameter("Next");
Previous = request.getParameter("Previous");
i2 = Integer.parseInt(i3);

j3 = request.getParameter("j2");
j2 = Integer.parseInt(j3);
count1 = request.getParameter("count");
count = Integer.parseInt(count1);




}
catch(Exception e){}


%>
<form method="post" action="http://localhost:8080/examples/jsp/img.jsp">
<%
ResultSet rs=null;
Connection con =null;
Statement stmt=null;
Vector v =new Vector();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("Jdbc:Odbc:chaitu");
stmt =con.createStatement();
String sql = "select img from image";
count++;

rs =stmt.executeQuery(sql);

while(rs.next())
{
v.addElement(rs.getString(1));
}

}catch(ClassNotFoundException ce) {ce.printStackTrace();}
catch(SQLException se) {se.printStackTrace();}
catch(Exception e1){ System.out.println(e1+"gsjfsdgjfsgkf");}

%>
<%

if(count == 1)
{
%>
<table width="100%" border="0">
<tr>
<%
if(v.size() > 3)
{
for(;i2<j2;i2++)
{%>
<td width="33%" align="center">
<img src=<%=v.elementAt(i2)%> width=150 height=150>
<br>
<a href=JavaScript:show('<%=v.elementAt(i2)%>')><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2" color="#FF8204"><b>1027*680</b></font></a>
</td>
<%
}
%>
</tr>
<tr>
<td width=33%> </td>
<td width=33%> </td>
<td width=33% align="right"><a href = img.jsp?Next=Next&i2=<%=i2%>&j2=<%=j2%>&count=<%=count%>><b>Next</b></a></td>
</tr>
<%}

if(v.size() <= 3)
{
for(;i2<v.size();i2++)
{%>
<td width="33%" align="center">
<img src=<%=v.elementAt(i2)%> width=150 height=150>
<br>
<a href=JavaScript:show('<%=v.elementAt(i2)%>')><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2" color="#FF8204"><b>1027*680</b></font></a>
</td>
<%
}
%>
</tr>
<tr>
<td width=33%> </td>
<td width=33%> </td>
<td width=33%> </td>
</tr>
<%} %>

</table>
<%
}

%>
<%
try
{

if((count > 1) && (Next.equals("Next")))
{
%>
<table width="100%" border="0">
<tr>
<%
j2=j2+3;
%>
</tr>
<%
if(v.size() > j2)
{
for(;i2<j2;i2++)
{%>
<td width="33%" align="center">
<img src=<%=v.elementAt(i2)%> width=150 height=150>
<br>
<a href=JavaScript:show('<%=v.elementAt(i2)%>')><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2" color="#FF8204"><b>1027*680</b></font></a>
</td>
<%
}
%>
<tr>
<td width=33%> </td>
<td width=33% align="right"> </td>
<td width=33% align="right">
<a href = img.jsp?Next=Next&i2=<%=i2%>&j2=<%=j2%>&count=<%=count%>><b>Next</b></a>  
<a href = img.jsp?Previous=Previous&i2=<%=i2%>&j2=<%=j2%>&count=<%=count%>><b>Previous</b></a>
</td>
</tr>
<%} %>
<%
if((v.size() < j2) | | (v.size() == j2))
{
for(;i2<v.size();i2++)
{
System.out.println("i2="+i2);
%>
<td width="33%" align="center">
<img src=<%=v.elementAt(i2)%> width=150 height=150>
<br>
<a href=JavaScript:show('<%=v.elementAt(i2)%>')><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2" color="#FF8204"><b>1027*680</b></font></a>
</td>
<%
}

%>
<tr>
<td width=33%> </td>
<td width=33%> </td>
<td width=33% align="right"><a href = "img.jsp?Previous=Previous&i2=<%=j2%>&j2=<%=j2%>&count=<%=count%>"><b>Previous</b></a></td>
</tr>
<%}%>

</table>
<%
}
}
catch(Exception e1){ System.out.println(e1+"gsjfsdgjfsgkf");}
%>
<%
try
{

if((count > 1) && (Previous.equals("Previous")))
{
%>
<table width="100%" border="0">
<tr>
<%
i2=i2-6;
j2=j2-3;
%>
</tr>
<%
if((v.size() > j2) && (j2 != 3))
{
for(;i2<j2;i2++)
{%>
<td width="33%" align="center">
<img src=<%=v.elementAt(i2)%> width=150 height=150>
<br>
<a href=JavaScript:show('<%=v.elementAt(i2)%>')><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2" color="#FF8204"><b>1027*680</b></font></a>
</td>
<%
}
%>
<tr>
<td width=33%> </td>
<td width=33% align="right"> </td>
<td width=33% align="right">
<a href = img.jsp?Next=Next&i2=<%=i2%>&j2=<%=j2%>&count=<%=count%>><b>Next</b></a>  
<a href = img.jsp?Previous=Previous&i2=<%=i2%>&j2=<%=j2%>&count=<%=count%>><b>Previous</b></a>
</tr>
<%} %>
<%
if(j2 == 3)
{
for(;i2<j2;i2++)
{%>
<td width="33%" align="center">
<img src=<%=v.elementAt(i2)%> width=150 height=150>
<br>
<a href=JavaScript:show('<%=v.elementAt(i2)%>')><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="2" color="#FF8204"><b>1027*680</b></font></a>
</td>
<%
}

%>
<tr>
<td width=33%> </td>
<td width=33%> </td>
<td width=33% align="right"><a href = img.jsp?Next=Next&i2=<%=i2%>&j2=<%=j2%>&count=<%=count%>><b>Next</b></a></td>
</tr>
<%}%>

</table>
<%
}
}

catch(Exception e1){ System.out.println(e1+"gsjfsdgjfsgkf");}
%>
</form>
</body>
</html>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!