Eswaramoorthy
Code Works
0 Answer(s)      3 years and 2 months ago
Posted in : JSP-Servlet

Hi
The code provided is working fine along with the pagination . i edited the queries and that makes difference..
here is the code.
Thank you


Regards
Eswaramoorthy





<%@ 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", "eswar");
ResultSet rs1 = null;
ResultSet rs2 = null;
PreparedStatement ps1=null;
PreparedStatement ps2=null;

int showRows=5;
int totalRecords=20;
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 * FROM student limit "+iPageNo+","+showRows+"";
//String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit ";

ps1=conn.prepareStatement(query1);
rs1=ps1.executeQuery();
String query2="SELECT count(*) as cnt from student";
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>
<%
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="newjsp.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
<%
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<a href="newjsp.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
<%
}
else if(i<=totalPages){
%>
<a href="newjsp.jsp?iPageNo=<%=i%>"><%=i%></a>
<%
}
}
if(totalPages>totalRecords && i<totalPages){
%>
<a href="newjsp.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();
}
%>




















View Answers









Related Pages:
Code Works - JSP-Servlet
Code Works  Hi The code provided is working fine along with the pagination . i edited the queries and that makes difference.. here is the code. Thank you Regards Eswaramoorthy Pagination of JSP page
Works only for one row
Works only for one row   Hi, My below code is working only if there is a single row. could you please help me in doing it for all the rows retrieved from the database. <%@page import="java.util.concurrent.CountDownLatch
Works when run from desktop but....
Works when run from desktop but....  I used the code provided here for a pie chart using swing in Java, and when I saved it to my desktop, it ran fine, and worked as explained, but then I tried to add the same code to NetBeans
Why disable.this works in fireFox but n ot IE
Why disable.this works in fireFox but n ot IE  When I put this line of code in my program, it works beautifully in firefox, but does not submit when using IE <input type="submit" id="9" value="Generate Report" onclick
code
code  Heris the javascript code.It works ffine but i would like to know what is the meaning of line this.SetData=SetData and next three lines after this(in bold) because when i remove them and try calling the functions via object
How jQuery works?
How jQuery Works?      This section we will see how jQuery works with the help of simple program... from the server. So, jQuey is very useful tool. Let's see how it works
VoIP Works
VoIP Works How VoIP Works If you've never heard of VoIP...;   How VoIP Works-Busting Out of Long Distance Rates... over Internet Protocol, and how VoIP works is actually quite revolutionary because
How Struts Works
How Struts Works      ... helps us to interact with your controller without writing much java code... there is no need to hard code the module which will be called within a component. The one
How TCP/IP works ?
How TCP/IP works ?  hello,, Can some body tell me how TCP/IP works?   hii, TCP/IP is just a protocol that allows different nodes on the network/Internet to share resources and without TRANSMISSION CONTROL PROTOCOL
How GPS Vehicle Tracking System Works
tracking system works with the help of the components it is made... geo-coding, GSM area code/cell code decoded, door open/close, pressure, cut off
code for program
code for program  hello,I am new to java and need to get the code for a program,i've been trying all sorts of code and nothing works any help would... enclosed in inverted commas thank you. BlockquoteHARDWARE ITEMS CODE
java code - Java Beginners
java code  Hi !I have already built a program for chatting (with GUI's ),but it works only with single client and single server.Can you please send me a java code for chatting which works with multiple clients an single server
jsp code - JSP-Servlet
jsp code  how to count no of clicks made on a link and save it on database( using jsp)  Hi Friend, Try the following code: 1... it works
how to make this java mail works? - Java Beginners
how to make this java mail works?  Dear experts, Recently, I borrowed a book by William Broden and managed to get the source codes from the author himself. The problem is that netbean ide is sending many error message
java code - JSP-Servlet
java code  hi i have made an application in which i have a fuctionality. in which i get the location of xml files as links on jsp page and when they are clicked .they opn in xml format..it works fine when i run the server
I invoke the ResultSet methods afterLast and beforeFirst when the method next works.
I invoke the ResultSet methods afterLast and beforeFirst when the method next works.  Why can't I invoke the ResultSet methods afterLast and beforeFirst when the method next works
Error in a code. - Java Beginners
Error in a code.  This is the question I posted yesterday: "I'm.... I'm confused about where and what i should put into the overall code. Also I can't quite figure out how to write the code for the GUI. Could anyone please help
Turn ASP.NET controls into AJAX-enabled controls
;    Hi guys! I created a control that works like an AJAX container. Basically the way it works is that all the ASP.NET controls that you.... You will see that you can code the ASP.NET controls exactly the same way you
problem on server which works fine on another - Java Server Faces Questions
problem on server which works fine on another  I am having a problem when a tab is clicked, the action listener class is called
Code Help Needed
Code Help Needed  I am trying to help a buddy of mine with this program and I can't seem to figure it out. I am still working on it but any help... works in conjunction with the menu if(parseInt == 1) { System.out.println
Implementation code inside interfaces
Implementation code inside interfaces 2001-01-25 The Java Specialists' Newsletter [Issue 006] - Implementation code inside interfaces Author: Dr. Heinz..., with more lines of pure Java code than the JDK 1.3, and actually paying me
How Fleet management System works
If you have a fleet and you are wondering how fleet management system works or how can you can be benefited by managing your fleet, than there is no need to get anxious any more. Fleet management requires some skilled labor, software
Use while loop in jsp code
Use while loop in jsp code       While loop is a control flow statement that works... then checks the boolean condition. Before run this jsp code create a new directory
Code
Code  code for connecting c lang to database
code
code    how to write this in java
code
code  code for android sample program
code for this is
code for this is  a b c d c b a a b c c b a a b b a a a code
Garbage Collection
Garbage Collection       In the example we are describing how the garbage collector works... and prints this time on the console. Here is the code of this program:  
code
code  please provide code for custom tags.by using currdate tag we need to get current date?please give me code
code
code   hi I need help in creating a java code that reminds user on a particular date about their festival. i have no clue of how to do it.. am looking forward to seek help from you
code
the correct code for a program.The output of the program is listed below...: {Block 5} ADDRESS-3: {San Juan} POST CODE:{6745} ENTER CODE (XX TO stop) CODE:{FF1} QUANTITY:{2} CODE:{TR4} QUANTITY:{1} CODE:XX INVOICE FOR YOUR ORDER Harry
code
code to create the RMI client on the local machine: import java.rmi.*; public... : "+e); } } } However, when the preceding code is executed it results... the correct code
code
code  i have four textboxes.whenever i click on up,down,left or down arrows then the cursor move to another textbox based on the key pressed.i want code for this in javascript
code
line of code to print the amount he should be paid as allowance
code
code  write a program to encrypt and decrypt the cipher text "adfgvx"   Hi Friend, Try the following code: import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.io.
How GPS Tracking System Works
Question arise in our mind "How does GPS Tracking system Works"? The Global Positioning system is a navigation system that is used to track objects like vehicles and mobiles. It is based on satellites, maps and computer software. People
How Ajax Works
How Ajax Works         How Ajax Works Ajax adds an extra layer of functionality in the communication model. Ajax engine acts
could anyone please help with the code.
could anyone please help with the code.  protected void doPost...(request, response); } } could anyone please check the code. If i enter... and password correctly then it works fine and if i enter it incorrectly once it again
How Web Hosting works?
How Web Hosting works?   ... the working of Web Hosting service. We will explain you How Web Hosting works? Web... and running. How Web Hosting works? When user types website address
How does Email works
How does Email works       Each Internet domain has email Server, when a user sends an Email… Email client Program sends the message to Email server. Email
How GPS Works?
How GPS Works?       Introduction GPS or Global Positioning System is a technology for locating.... The Idea behind GPS The GPS system works with the help of a satellite network
How Voice over Internet Protocol (VoIP) Works
. It is still a mystery for them the how do voice over IP (VoIP) works. But using VoIP servies is very easy once you know how it works. How VoIP Works? Voice

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.