Searching an element

Searching an element

Dear sir,
sir i have an employee table ,in that i have attributes like empId name ,salary,loc etc.I have one jsp in that i am doing a searching an element by empId and displaying a all the employee table with pagination.I have a 2 forms in one jsp.I have a one input text field for searching an element by the empId.and one submit button for submitting this form.And another form for displaying a all employee table ,it will display all employees.If i dont enter any thing in a text field ,then the table should show a all employees details with pagination.If i give a any empId in a text field then submitted ,the table show only a identical empid records with pagination .so how to do this sir please help me..

Thanks and Regards
Harini Veerapur.
View Answers

June 19, 2010 at 11:36 AM

Hi Friend,

Try the following code:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<form method="post" action="pagination.jsp">
Enter ID: <input type="text" name="id"><input type="submit">
</form>

<%
String query1="",query2="";
String ide=request.getParameter("id");

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=10;
int totalRecords=10;
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);
}
if(ide==null){
query1="SELECT SQL_CALC_FOUND_ROWS * FROM employee limit "+iPageNo+","+showRows+"";
query2="SELECT FOUND_ROWS() as cnt";
}
else if(ide!=null){
query1="SELECT SQL_CALC_FOUND_ROWS * FROM employee where userid='"+ide+"' limit "+iPageNo+","+showRows+"";
query2="SELECT FOUND_ROWS() as cnt";
}
ps1=conn.prepareStatement(query1);
rs1=ps1.executeQuery();
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>
<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>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Address</td>
</tr>
<%
while(rs1.next()){
%>
<tr>
<td><%=rs1.getString("firstname")%></td>
<td><%=rs1.getString("lastname")%></td>
<td><%=rs1.getString("email")%></td>
<td><%=rs1.getString("address")%></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>

June 19, 2010 at 11:36 AM

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="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
<%
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<a href="pagination.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
<%
}
else if(i<=totalPages){
%>
<a href="pagination.jsp?iPageNo=<%=i%>"><%=i%></a>
<%
}
}
if(totalPages>totalRecords && i<totalPages){
%>
<a href="pagination.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> Next</a>
<%
}
}
%>
<b>Rows <%=startResult%>-<%=endResult%>Total Rows<%=totalRows%> </b>
</div>
</td>
</tr>
</table>
</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();
}
%>
<%!
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;
}
%>

Thanks









Related Tutorials/Questions & Answers:
Searching an element - JSP-Servlet
in that i am doing a searching an element by empId and displaying a all... field for searching an element by the empId.and one submit button for submitting...Searching an element  Dear sir, sir i have an employee
Searching an element - JSP-Servlet
doing a searching an element by empId and displaying a all the employee table... for searching an element by the empId.and one submit button for submitting this form.And...Searching an element  Dear sir, sir i have an employee table
Advertisements
Searching an Element in the given XML Document
Searching an Element in the given XML Document       In this you will learn to search an element.... This program asks for a  xml element name that have to be searched
searching books
searching books  how to write a code for searching books in a library through jsp
searching technique
searching technique  Hi, i need any searching technique in java to search the data and give the all possible data by links
searching problem
searching problem  how we can make a code in core java for searching in hard disk? i have to make an antivirus as my minor project , so i have need this type code to search the virus in hard disk
searching problem
searching problem  how we can make a code in core java for searching in hard disk? i have to make an antivirus as my minor project , so i have need this type code to search the virus in hard disk
file searching
file searching  i need a prog for searching inside a text file after evry 1 min to find does it hav any new entry
searching string
searching string   how to write a program for searching a string   Hi Friend, Try this: import java.util.*; class SearchString{ public static void main(String[] args){ Scanner input=new Scanner(System.in
searching the data
searching the data  respected sir , i am making the desktop application , so how can i search the data which are stored in the mysql databses through click the button and display in jtable through netbeans dynamically
searching inside a file for details
searching inside a file for details  i created a file with name,country,state etc...when i'm searching for the name, i have to get other details like country,state etc...please do help
searching the string in a paragraph?
searching the string in a paragraph?  how to search a string in a paragraph and display the corresponding line number and position of a string in java
Searching for Code - JSP-Servlet
JSP, Servlet Searching for Code  Hi, i am looking for a jsp servlet code examples for the search function in my application.Thanks
searching from database
searching from database  how to search data from data base throug search...such as contact no./lastname /firstname should give whole information from database...   Please visit the following link: Servlet search
Java String searching.
Java String searching.  How to search a string in given String array?   import java.util.Scanner; public class StringSearch { public... and for searching we used userCity.equalsIgnoreCase(city[i
Java Sorting and Searching
Java Sorting and Searching   If anyone could help me with this I would be really thankful! Write a program that stores the names of these artists in a String array. The program should prompt the user to enter the name
Searching English words in a string
Searching English words in a string  My task is to find English words and separate them from a string with concatenated words..for example AhdgdjHOWAREgshshYOUshdhfh I need to find if there exists any English words.   
Searching operation in JAva
Searching operation in JAva  Sir, I have made a project for the Dispatch section of my office. I have to search for letters that are dispatched on a particular date. Now there are thousands of letters dispatched on a particular
Searching operation in JAva
Searching operation in JAva  Sir, I have made a project for the Dispatch section of my office. I have to search for letters that are dispatched on a particular date. Now there are thousands of letters dispatched on a particular
Searching operation in JAva
Searching operation in JAva  Sir, I have made a project for the Dispatch section of my office. I have to search for letters that are dispatched on a particular date. Now there are thousands of letters dispatched on a particular
Searching operation in JAva
Searching operation in JAva  Sir, I have made a project for the Dispatch section of my office. I have to search for letters that are dispatched on a particular date. Now there are thousands of letters dispatched on a particular
searching for strings in a file
searching for strings in a file  how can i search of a person and display the other details associated with the name using java gui window?   import java.util.ArrayList; import javax.swing.*; import java.awt.event.
Insertion, sorting and searching in array
; } Insertion, sorting and searching in array   ... to perform searching and sorting in array. In the java code given below we have...() method, also we defined two methods to show the element of the array and insert
remove parent element JavaScript
remove parent element JavaScript  How to remove parent element in JavaScript
Finding searching phrase of a search engine
Finding searching phrase of a search engine  how to find out searching phrase of a search engine..? like, if visitors enter the keyword to google, is is any possible to get that keyword
JQuery-Disabling element in JQuery.
JQuery-Disabling element in JQuery.   How can we enable/disable an element in JQuery
ModuleNotFoundError: No module named 'element'
ModuleNotFoundError: No module named 'element'  Hi, My Python... 'element' How to remove the ModuleNotFoundError: No module named 'element... to install padas library. You can install element python with following command
Searching with alphabetical order - Java Beginners
Searching with alphabetical order  Hi, I want to this please help me Steps:- 1:-I have a one form of name text box with two button. 2:-user input in name text box(alphabetical order)search all fields from
Searching with alphabetical order - Java Beginners
Searching with alphabetical order  Hi, please understood my problem and send write code its very urgent I a write once again. Steps.... 2:-User input in the name text box(searching alphabetical order) name
Searching Mobile developer for image recognition
Searching Mobile developer for image recognition  Hi, I have an idea for an application that requires image recognition. I have decided to use Google platform for image recognition. Our application will provide image to google
JQuery-confirming exsistance of an element.
JQuery-confirming exsistance of an element.  Hi there, How do I test whether an element exists in jQuery?   You can check existance of an element in jQuery using length property of the jQuery collection
protractor check if element exists
protractor check if element exists  Hi, We are working... to check if element exists in protractor framework? Thanks   Hi, There are methods protractor to check if element exists. You can use the following
XML root element
XML root element  Hi..... please tell me about that Can a root element type be explicitly declared in the DTD?ADS_TO_REPLACE_1 Thanks
Passing array element
Passing array element  What about passing an element of an array? If I have array of objects and pass functionA(arr[2]); Then functionA() will get arr[2] as a value type or a reference type
persistence xml class element
persistence xml class element  Hi, How to define class element in persistence.xml file. Thanks   Hi, You can use the .... tag. Here is the example: <class>net.roseindia.model.Product</class> Read more
JavaScript Remove Element
JavaScript Remove Element...; In this section, you will learn how to remove HTML element using JavaScript. JavaScript provides several methods which allow the user to remove particular HTML element
set the focus on an element using Javascript
set the focus on an element using Javascript  How to set the focus on an element using Javascript
Searching For Important Issues for Writing Articles
Searching For Important Issues for Writing Articles       People all over the world like to read... for their art of creating key issues out of scrap. Whenever you are searching
Searching - Hibernate
Sorting and Searching
Sort the element find mean
Sort the element find mean  Hi Friend, I want to find meaning of some real numbers like, if we have numbers- 52.15, 89.0, 314.48, 845.75, 309.11, 575.36, 398.98, 318.64, 2485.12, 377.44 Then I want to find
Array element comparison
Array element comparison  I have arrays of Length(L) , width(W) and Height(H) of same size(in length). Here L , W and H are entered by user. Eg:- Assuming size as 5. Therefore ; Array 1 be 10 , 5 , 6 Array 2 be 12 , 6 , 8 Array 3
return the position of last occurrence of an element
" findLast(E element) " returns the position of the last occurrence of the given element. If this element is not in the list, then the null reference should... towards the beginning, terminating when the element is found or the beginning
return the position of last occurrence of an element
" findLast(E element) " returns the position of the last occurrence of the given element. If this element is not in the list, then the null reference should... towards the beginning, terminating when the element is found or the beginning
searching a text string in file - Java Beginners
searching a text string in file  code to match a string in a file that is already mentioned in java code.if the string matches any part of a string that whole string is printed
Searching a word file on server in JSP - JSP-Servlet
Searching a word file on server in JSP  Seacrhing a word file on server side using JSP. I am Apache Tomcat Server. Please help me Sir
Java: Searching a string from doc files
Java: Searching a string from doc files   I want to search a string from DOC, DOCX files in java. Can anyone please help me
ModuleNotFoundError: No module named 'toro-element'
ModuleNotFoundError: No module named 'toro-element'  Hi, My Python... 'toro-element' How to remove the ModuleNotFoundError: No module named 'toro-element' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'element-ai'
ModuleNotFoundError: No module named 'element-ai'  Hi, My Python... 'element-ai' How to remove the ModuleNotFoundError: No module named 'element-ai' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'element-py'
ModuleNotFoundError: No module named 'element-py'  Hi, My Python... 'element-py' How to remove the ModuleNotFoundError: No module named 'element-py' error? Thanks   Hi, In your python environment you

Ads