Code Works

Ads

 
 

Share on Google+Share on Google+

Eswaramoorthy
Code Works
0 Answer(s)      7 years 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();
}
%>




















Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
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
Maven Dependency works-bolts >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-bolts version1.0.0 in your project
Maven Dependency works-bolts >> 1.0.4
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-bolts version1.0.4 in your project
Maven Dependency works-builder >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-builder version1.0.0 in your project
Maven Dependency works-http >> 1.0
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0 in your project
Maven Dependency works-http >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0.0 in your project
Maven Dependency works-http >> 1.0.1
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0.1 in your project
Maven Dependency works-http >> 1.0.2
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0.2 in your project
Maven Dependency works-http >> 1.0.3
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0.3 in your project
Maven Dependency works-http >> 1.0.4
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0.4 in your project
Maven Dependency works-http >> 1.0.5
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-http version1.0.5 in your project
Maven Dependency works-util >> 1.0
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-util version1.0 in your project
Maven Dependency works-util >> 1.0.1
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-util version1.0.1 in your project
Maven Dependency works-util >> 1.0.2
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-util version1.0.2 in your project
Maven Dependency works-util >> 1.0.3
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-util version1.0.3 in your project
Maven Dependency works-util >> 1.0.4
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-util version1.0.4 in your project
Maven Dependency works-widget >> 1.0
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-widget version1.0 in your project
Maven Dependency works-widget >> 1.0.1
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-widget version1.0.1 in your project
Maven Dependency works-widget >> 1.0.2
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-widget version1.0.2 in your project
Maven Dependency works-widget >> 1.0.3
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-widget version1.0.3 in your project
Maven Dependency works-widget >> 1.0.4
You should include the dependency code given in this page to add Maven Dependency of com.mobilesolutionworks >> works-widget version1.0.4 in your project
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
Version of com.mobilesolutionworks>works-bolts dependency
List of Version of com.mobilesolutionworks>works-bolts dependency
Version of com.mobilesolutionworks>works-builder dependency
List of Version of com.mobilesolutionworks>works-builder dependency
Version of com.mobilesolutionworks>works-http dependency
List of Version of com.mobilesolutionworks>works-http dependency
Version of com.mobilesolutionworks>works-util dependency
List of Version of com.mobilesolutionworks>works-util dependency
Version of com.mobilesolutionworks>works-widget dependency
List of Version of com.mobilesolutionworks>works-widget dependency
Version of com.mobilesolutionworks>works-bolts dependency
List of Version of com.mobilesolutionworks>works-bolts dependency

Ads

 
Advertisement null

Ads