<%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%!public int nullIntconv(String str) {
int conv = 0;
if (str == null) {
str = "0";
} else if ((str.trim()).equals("null")) {
str = "0";
} else if (str.equals("")) {
str = "0";
}
try {
conv = Integer.parseInt(str);
} catch (Exception e) {
}
return conv;
}%>
<%! //global declaration of all the variables
int iShowRows = 5; // Number of records show on per page
int iTotalSearchRecords = 5; // Number of pages index shown
int iTotalRows = 0; // total rows returned
int iTotalPages = 0; // total number of pages
int iPageNo = 0;
int cPageNo = 0;
int iStartResultNo = 0;
int iEndResultNo = 0;
Connection conn = null;
ResultSet rsPagination = null;
ResultSet rsRowCnt = null;
PreparedStatement psPagination = null;
PreparedStatement psRowCnt = null; %>
<%
//connecting with the database
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jbbcracle:thin:@localhost:1521:XE","system", "elan");
} catch (Exception e) {
System.out.println("XXXXXX exception in creating sql connection");
}
try {
iTotalRows = nullIntconv(request.getParameter("iTotalRows"));
iTotalPages = nullIntconv(request.getParameter("iTotalPages"));
iPageNo = nullIntconv(request.getParameter("iPageNo"));
cPageNo = nullIntconv(request.getParameter("cPageNo"));
}
catch (Exception e) {
System.out.println("XXXX exception in the getparameter ");
}
try {
if (iPageNo == 0) {
iPageNo = 0;
} else {
iPageNo = Math.abs((iPageNo - 1) * iShowRows);
}
} catch (Exception e) {
System.out.println("XXXX exception in the math.abs area ");
}
%>
<%
//// this will count total number of rows
String sqlRowCnt = "SELECT count from user_reg_details";
psRowCnt = conn.prepareStatement(sqlRowCnt);
rsRowCnt = psRowCnt.executeQuery();
while (rsRowCnt.next()) {
iTotalRows = rsRowCnt.getInt(1);
}
//// calculate next record start record and end record
try {
if (iTotalRows < (iPageNo + iShowRows)) {
iEndResultNo = iTotalRows;
} else {
iEndResultNo = (iPageNo + iShowRows);
}
iStartResultNo = (iPageNo + 1);
iTotalPages = ((int) (Math
.ceil((double) iTotalRows / iShowRows)));
} catch (Exception e) {
System.out.println("XXXX Exception in calculating the next record");
e.printStackTrace();
}
%>
<%
System.out.println("IpageNo =" + iPageNo + "ishowrows = "+ iShowRows);
String sqlPagination = "SELECT * FROM ( SELECT t.*, ROWNUM AS rn FROM ( SELECT * FROM USER_REG_DETAILS ) t )WHERE rn BETWEEN ";
sqlPagination += iStartResultNo + " AND " + iEndResultNo + "";
psPagination = conn.prepareStatement(sqlPagination);
rsPagination = psPagination.executeQuery();
%>
<html>
<head>
<title>Pagination of JSP page</title>
</head>
<body>
<form name="frm">
<input type="hidden" name="iPageNo" value="<%=iPageNo%>">
<input type="hidden" name="cPageNo" value="<%=cPageNo%>">
<input type="hidden" name="iShowRows" value="<%=iShowRows%>">
<table width="60%" cellpadding="0" cellspacing="0" border="1" >
<tr bgcolor="grey" style="color: white;">
<td>Name</td>
<td>Batch</td>
<td>Address</td>
</tr>
<%
while (rsPagination.next()) {
%>
<tr >
<td><%=rsPagination.getString(1)%></td>
<td><%=rsPagination.getString(2)%></td>
<td><%=rsPagination.getString(3)%></td>
</tr>
<%
}
%>
</table>
<table>
<tr>
<td colspan="3">
<div>
<%
//// index of pages
int i=0;
int cPage=0;
if(iTotalRows!=0)
{
cPageint)(Math.ceil((double)iEndResultNo/(iTotalSearchRecords*iShowRows))));
int prePageNo= +(cPage*iTotalSearchRecords)-((iTotalSearchRecords-1)+iTotalSearchRecords);
System.out.println(" pre " + prePageNo );
if((cPage*iTotalSearchRecords)-(iTotalSearchRecords)>0)
{
%>
<a href="Pagination Implementation.jsp?iPageNo=<%=prePageNo%>&cPageNo= <%=prePageNo%>"> << Previous</a>
<%
}
for(icPage*iTotalSearchRecords)-(iTotalSearchRecords-1));i<=(cPage*iTotalSearchRecords);i++)
{
if(i=iPageNo/iShowRows)+1))
{
%>
<a href="Pagination Implementation.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
<%
}
else if(i<=iTotalPages)
{
%>
<a href="Pagination Implementation.jsp?iPageNo=<%=i%>"><%=i%></a>
<%
}
}
if(iTotalPages>iTotalSearchRecords && i<=iTotalPages )
{
%>
<a href="Pagination Implementation.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >>Next</a>
<%
}
}
%>
<HR></HR>
<% System.out.println("total number of pages" + iTotalPages);%>
<b>Rows <%=iStartResultNo%> - <%=iEndResultNo%> Total Result <%=iTotalRows%> </b>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
<%
try {
if (psPagination != null) {
psPagination.close();
}
if (rsPagination != null) {
rsPagination.close();
}
if (psRowCnt != null) {
psRowCnt.close();
}
if (rsRowCnt != null) {
rsRowCnt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
%>