Runtime paging with java for the database values.

I m preparing a project in which i cant try to page the data etracted from the database in runtime. Plz Help me for this problem.
[136 byte] By [Vivek@Bhartia] at [2007-11-26 16:46:32]
# 1

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.DriverManager;

import java.util.List;

import java.util.ArrayList;

import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

public class AttachFormPagination extends Action

{

public AttachFormPagination()

{}

public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception

{

try

{

int tot = 0;

String startIndex = request.getParameter("start");

String endIndex = request.getParameter("end");

System.out.println("start is>" + startIndex);

System.out.println("end is>" + endIndex);

int sIndex = Integer.valueOf(startIndex).intValue();

int eIndex = Integer.valueOf(endIndex).intValue();

Class.forName("org.gjt.mm.mysql.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost/","id","pw");

Statement st = con.createStatement();

// this is for getting the total records

Statement st1 = con.createStatement();

ResultSet rs1 = st1.executeQuery("select * from tablename");

while(rs1.next())

{

tot++;

}

System.out.println("total value is" + tot);

String total = ""+tot;

String query = "select * from tablename limit " + sIndex + "," + eIndex;

System.out.println("query is>" + query);

ResultSet rs = st.executeQuery(query);

List cusList = new ArrayList();

AttachFormBean afb = null;

while(rs.next())

{

afb = new AttachFormBean();

afb.setName(rs.getString(1));

afb.setActivity(rs.getString(2));

afb.setProduct(rs.getString(3));

afb.setCompetitor(rs.getString(4));

afb.setEnvironment(rs.getString(5));

afb.setCreatedBy(rs.getString(6));

cusList.add(afb);

}

request.setAttribute("custlist",cusList);

request.setAttribute("startIndex",startIndex);

request.setAttribute("endIndex",endIndex);

request.setAttribute("total",total);

}

catch (Exception e)

{

System.out.println("Exception"+e);

}

return mapping.findForward("success");

}

}

and

this code to be placed in ur page

<table border="0" cellpadding="0" cellspacing="0" width="100%">

<tr>

<td align="left">?lt;/td>

<td nowrap align="right">

<a href="#" onclick="PagenationStart()">

<img src='images/start_off.gif' width='9' height='8' alt='Start' border='0' align='absmiddle' href="#" >燬tart牋</a>

<a href="#" onclick="PagenationPrevious()">

<img src='images/previous_off.gif' width='4' height='8' alt='Previous' border='0' align='absmiddle'>燩revious牋</a>

<a href="#" onclick="PagenationNext()">

牋Next?lt;img src='images/next_off.gif' width='4' height='8' alt='Next' border='0' align='absmiddle'></a>

<a href="#" onclick="PagenationEnd()">

牋End?lt;img src='images/end_off.gif' width='9' height='8' alt='End' border='0' align='absmiddle'></a></td>

</tr>

</table>

and put this in ur page inside script

function PagenationStart()

{

var a = 0;

var b = 4;

document.ownerPage.action="actionpagename.do?param="+param+"&start="+a+"&end="+b;

document.ownerPage.submit();

}

function PagenationPrevious()

{

var a = eval('<c:out value="${requestScope.startIndex}"/>') - 4;

var b = 4;

document.ownerPage.action="actionpagename.do?param="+param+"&start="+a+"&end="+b;

document.ownerPage.submit();

}

function PagenationNext()

{

var a = eval('<c:out value="${requestScope.startIndex}"/>')+4;

var b = 4;

document.ownerPage.action="actionpagename.do?param="+param+"&start="+a+"&end="+b;

document.ownerPage.submit();

}

function PagenationEnd()

{

var a = eval('<c:out value="${requestScope.total}"/>')-4;

var b = eval('<c:out value="${requestScope.total}"/>');

document.ownerPage.action="actionpagename.do?param="+param+"&start="+a+"&end="+b;

document.ownerPage.submit();

}

uk0102a at 2007-7-8 23:13:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...