Home Answers Viewqa JSP-Servlet probliem in pagiation

 
 


Eswaramoorthy
probliem in pagiation
3 Answer(s)      3 years and 2 months ago
Posted in : JSP-Servlet

Hi
the code provided for displaying editable datagrid in jsp works fine.
but there is some pagination problem.when my sql data are more then 5 record,
the link for 2 nd page doesn't work.

Regards
Eswaramoorty

here is the provided code
http://www.roseindia.net/answers/viewanswers/9032.html
View Answers

March 23, 2010 at 3:17 PM


u have to use the diplay tag s in jsp page for example:don;t use script code ,just u an provide property name.


<display:column ...........

March 23, 2010 at 3:28 PM


Hi Friend,

Try the following code:

1)datagrid.jsp:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%!
public int nullIntconvert(String str){
int num=0;
if(str==null) {
str="0";
}
else if((str.trim()).equals("null")) {
str="0";
}
else if(str.equals("")) {
str="0";
}
try{
num=Integer.parseInt(str);
}
catch(Exception e) { }
return num;
}
%>
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
ResultSet rs1 = null;
ResultSet rs2 = null;
PreparedStatement ps1=null;
PreparedStatement ps2=null;

int showRows=5;
int totalRecords=5;
int totalRows=nullIntconvert(request.getParameter("totalRows"));
int totalPages=nullIntconvert(request.getParameter("totalPages"));
int iPageNo=nullIntconvert(request.getParameter("iPageNo"));
int cPageNo=nullIntconvert(request.getParameter("cPageNo"));

int startResult=0;
int endResult=0;
if(iPageNo==0){
iPageNo=0;
}
else{
iPageNo=Math.abs((iPageNo-1)*showRows);
}
String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit "+iPageNo+","+showRows+"";
ps1=conn.prepareStatement(query1);
rs1=ps1.executeQuery();
String query2="SELECT FOUND_ROWS() as cnt";
ps2=conn.prepareStatement(query2);
rs2=ps2.executeQuery();
if(rs2.next()) {
totalRows=rs2.getInt("cnt");
System.out.println(totalRows);
}
%>
<html>
<h3>Pagination of JSP page</h3>
<body>
<form method="post" action="upgrid.jsp">
<input type="hidden" name="iPageNo" value="<%=iPageNo%>">
<input type="hidden" name="cPageNo" value="<%=cPageNo%>">
<input type="hidden" name="showRows" value="<%=showRows%>">
<table width="100%" cellpadding="0" cellspacing="0" border="1" >
<tr>
<td>Roll No</td>
<td>Name</td>
<td>Marks</td>
<td>Grade</td>
</tr>
<%
while(rs1.next()){
%>
<tr>
<td><input type="text" name="no" size="38" value="<%=rs1.getInt("rollNo")%>"></td>
<td><input type="text" name="name" size="38" value="<%=rs1.getString("name")%>"></td>
<td><input type="text" name="marks" size="38" value="<%=rs1.getInt("marks")%>"></td>
<td><input type="text" name="grade" size="38" value="<%=rs1.getString("grade")%>"></td>
</tr>
<%
}
%>
<%
try{
if(totalRows<(iPageNo+showRows)) {
endResult=totalRows;
}
else{
endResult=(iPageNo+showRows);
}
startResult=(iPageNo+1);
totalPages=((int)(Math.ceil((double)totalRows/showRows)));
}
catch(Exception e){
e.printStackTrace();
}
%>
<tr>
<td colspan="3">
<div>

March 23, 2010 at 3:29 PM


continue..

<%
int i=0;
int cPage=0;
if(totalRows!=0) {
cPage=((int)(Math.ceil((double)endResult/(totalRecords*showRows))));
int prePageNo=(cPage*totalRecords)-((totalRecords-1)+totalRecords);
if((cPage*totalRecords)-(totalRecords)>0){
%>
<a href="datagrid.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
<%
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<a href="datagrid.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
<%
}
else if(i<=totalPages){
%>
<a href="datagrid.jsp?iPageNo=<%=i%>"><%=i%></a>
<%
}
}
if(totalPages>totalRecords && i<totalPages){
%>
<a href="datagrid.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> Next</a>
<%
}
}
%>
<b>Rows <%=startResult%>-<%=endResult%>Total Rows<%=totalRows%> </b>
</div>
</td>
</tr>
</table>
<input type="submit" value="Update">
</form>
</body>
</html>
<%
try{
if(ps1!=null){
ps1.close();
}
if(rs1!=null){
rs1.close();
}

if(ps2!=null){
ps2.close();
}
if(rs2!=null){
rs2.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>

2)upgrid.jsp:

<%@ page import="java.sql.*" %>
<%
String d[] =request.getParameterValues("no");
if(d !=null) {
for(int i=0;i<d.length;i++) {
out.println(d[i]);
}
}
String name[] =request.getParameterValues("name");
if(name !=null) {
for(int i=0;i<name.length;i++) {
out.println(name[i]);
}
}
String marks[] =request.getParameterValues("marks");
if(marks !=null) {
for(int i=0;i<marks.length;i++) {
out.println(marks[i]);
}
}
String grade[] =request.getParameterValues("grade");
if(grade !=null) {
for(int i=0;i<grade.length;i++) {
out.println(grade[i]);
}
}
%>
<%
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");

Statement st=null;
st=conn.createStatement();
for(int a=0;a<30;a++){
String no=d[a];
String n=name[a];
String mar=marks[a];
String g=grade[a];
st.executeUpdate("update student set name='"+n+"', marks='"+mar+"', grade='"+g+"' where rollNo='"+no+"'");
}
}
catch(Exception e){
e.printStackTrace();
}
%>

Thanks









Related Pages:
probliem in pagiation - JSP-Servlet
probliem in pagiation  Hi the code provided for displaying editable datagrid in jsp works fine. but there is some pagination problem.when my sql data are more then 5 record, the link for 2 nd page doesn't work. Regards

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.