HTML GRID

HTML GRID

how to retrieve data from mysql database in grid form using html and servlets? urgent....

View Answers

November 8, 2011 at 11:39 AM

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PaginationInServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
Connection conn = null;
try{
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);
}
out.println("<html><h3>Pagination of JSP page</h3><body><form>");
out.println("<input type='hidden' name='iPageNo' value='<%=iPageNo%>'>");
out.println("<input type='hidden' name='cPageNo' value='<%=cPageNo%>'>");
out.println("<input type='hidden' name='showRows' value='<%=showRows%>'>");
out.println("<table width='100%' cellpadding='0' cellspacing='0' border='1' >");
out.println("<tr><td>Roll No</td><td>Name</td><td>Marks</td><td>Grade</td></tr>");
while(rs1.next()){
out.println("<tr><td>"+rs1.getInt("rollNo")+"</td><td>"+rs1.getString("name")+"</td><td>"+rs1.getInt("marks")+"</td><td>"+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();
}
out.println("<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){
out.println("<a href=PaginationInServlet?iPageNo="+prePageNo+"&cPageNo="+prePageNo+"'> << Previous</a>");
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){
out.println("<a href=PaginationInServlet?iPageNo="+i+"style=cursor:pointer;color: red><b>"+i+"</b></a>");
}
else if(i<=totalPages){
out.println("<a href=PaginationInServlet?iPageNo="+i+">"+i+"</a>");
}
}
if(totalPages>totalRecords && i<totalPages){
out.println("<a href=PaginationInServlet?iPageNo="+i+"&cPageNo="+i+"> >> Next</a>");
}
}
out.println("<b>Rows "+startResult+"-"+endResult+"Total Rows"+totalRows+"</b></div></td></tr></table></form></body></html>");

November 8, 2011 at 11:39 AM

continue..

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();
}
}
catch(Exception ex){}
}
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;
}
}

For the above code, we have used following database table:

CREATE TABLE `student` (
`rollNo` bigint(40) NOT NULL auto_increment,
`name` varchar(40) default NULL,
`marks` varchar(40) default NULL,
`grade` varchar(40) default NULL,
PRIMARY KEY (`rollNo`)
)









Related Tutorials/Questions & Answers:
HTML GRID
HTML GRID  how to retrieve data from mysql database in grid form using html and servlets? urgent....   import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class
Html
Html  in the web page if judge select the age and category then automatically under that conditions the details will be display in the form of grid give me that code in html and java
Advertisements
HTML
HTML  I want the following fields as in the form of grid view Fields... of the work,image of the work,points from the judges and above of this grid form i... and age buttons then automatically displays the grid form and display the values
HTML
HTML  how can we design the gird form view in html i want the following fields in the grid form The fields are Name of the candidate,Title... me the coding for this in html
HTML
HTML  How can we increase the row size in the grid form view of the table.I want to store the maximum 4 details of the candidate in one page of the grid table view
HTML
HTML  What is Semantic HTML? What are the reasons for validating a HTML
HTML
HTML  What is Semantic HTML? What are the reasons for validating a HTML
HTML
HTML  What is SPAN in HTML
HTML
HTML  What is HTML? What is a Hypertext link
HTML
HTML  What is HTML? What is a Hypertext link
Html
Html  What is HTML? What is a Hypertext link
HTML
HTML  What is BODY in HTML document
html
html  How to add the calendar in html code
html
html  diffrence between html and xml
html
add a header in the html page  what we can do to add a header in the html page
HTML
HTML  How do we specify page breaks in HTML
HTML
HTML  What is a tag? How can we use MARQUEE in HTML
html
html  how to convert html page into jsp page
html
html  i want a registration page in html with good background
HTML
HTML  how can we give the unlimited size in the html
html
html  what we can do to add a header in the html page
html
html  what we can do to add a header in the html page
HTML
HTML  Hi, What is the full form of HTML? Thanks (adsbygoogle = window.adsbygoogle || []).push({});   Hi, The HTML stands for HyperText Markup Language. HTML is used to create webpage for world wide web
html
html  sir i am new to eclipse and i am created a html program and also tomcat is running successfully in side the eclipse.but now how i deploy that html program in the eclipse
html
html  Can we Access database through html page and how,if not then why
HTML
HTML  How do we create a link? What are the three types of form tags in HTML
html
html  how we can give the form actions in html after register button in the login page
html
Adding Header in HTML  what we can do to add a header in the html page
html
html  For what is used HTML?   Using HTML we can create a Web Page which can be viewed through a Web Browser over Internet. HTML describes... or the End Tag is defined as </> . The whole HTML document is enclosed
html
html   write a html program to print "HELLO WORD"in red coaler font size 12 and italic stile   Here is a html code that display the message "Hello World" in red font with size 12 and in italics. <html> <
Html
Html  i am select the dateofbirth through the html browser.but it can not save into the sql server 2008 database.any special code required to store the date in database by using java servlets
HTML
HTML  I have created an info.html file for the applet.The class file is located in a directory different from the HTML file.The info.html file I have created is below. <HTML> <HEAD> <BODY> <APPLET Code
HTML
HTML  in html how can we can upload the file <tr> <td><strong>Video of the work:</strong> <td><b>... the file give me the code for this in html and java
html
html  Create a web page with the following using HTML i) To embed an image map in a web page ii) To fix the hot spots iii) Show all the related information when the hot spots are clicked.please reply me sir
Html
Html  I was designing a webpage, i need to create selection boxes one beside other, how i'll be able to do it using HTML code, Please give me a sample code for that... looking forward for your usefull code
Html
:48:44 PM Author : SAMSUNG --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...; <head> <meta http-equiv="Content-Type" content="text/html
Html
:48:44 PM Author : SAMSUNG --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...; <head> <meta http-equiv="Content-Type" content="text/html
Html
Html  in html form for the address by using input as textarea i am writing the code as <tr> <td valign="top"><strong>Address: </strong></td> <td><textarea name="Address" rows="8
html
html   HTML document with Javascript to count the number of vowels in a text typed in text area. please send the code.   Hi Ravi Here... HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD>
Html
Html  can we send data from one html page to another html page? if yes how?   Yes, using javascript. 1)page1.html: <html> <form type=get action="page2.html"> <table> <tr> <td>First Name
HTML
it shows as null. here is the html code for date of birth: <tr> <
Ajax grid
Ajax grid  Hi, How I can create ajax grid in my program? Thanks
Grid Layout Container in Flex4
Grid Layout Container in Flex4: The Grid Layout Container is a MX container. There is no Spark equivalent. It's like an HTML Table. You can arrange... in a row and each cell can have a Grid container. The default Horizontal and Vertical
Nitobi Grid is not working in IE10/IE11
Nitobi Grid is not working in IE10/IE11  Nitobi grid is working in IE8, but not working in IE10/IE11. we are using above two javascript files to open the grid
HTML
HTML       HTML is used for creating the web pages on Internet. HTML.... If you have to create a website you have to use the HTML for creating pages
ModuleNotFoundError: No module named 'grid'
ModuleNotFoundError: No module named 'grid'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'grid' How to remove the ModuleNotFoundError: No module named 'grid' error
Jsp Grid
="ISO-8859-1"%> &lt;%@ page contentType="text/html"%> &lt;%@ page import...()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional... http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> &lt
Sigma grid -- Ajax-enabled JavaScript grid
Sigma grid -- Ajax-enabled JavaScript grid       Sigma Grid Sigma Grid is a freely open source AJAX editable data grid, written in pure javascript for displaying and inline
HTML
HTML - Hypertext markup language tutorials Learn HTML with the help of many examples. We have created many easy to understand HTML examples. These examples will help you in understanding HTML very quickly HTML is used to create web
java + grid - Java3D
java + grid  i need to give the output of the application in the form of a grid in XY-plane,the grid should have different colors for regions of different value range

Ads