Home Answers Viewqa JSP-Servlet Searching an element

 
 


harini
Searching an element
2 Answer(s)      2 years and 11 months ago
Posted in : JSP-Servlet

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 Pages:
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
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... asks for a  xml element name that have to be searched in the XML document
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
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
Finding an Element in a Sorted Array
Finding an Element in a Sorted Array       In the sorted array, searching is very easy... an searching an elements in the array. This example takes some inputs from the user
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 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
Sorting Vector Element using I18N
Sorting Vector Element using I18N       This Example shows you how to sort vector element using I18N... to appends the Fixed element to the end of this Vector. Collator.getInstance
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 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 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 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.
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
Linear Search in Java
Linear Search in Java In this section, we are going to find an element from an array using Linear Searching. Linear searching is a good way to find an element... element (number , string , etc. ) is in a specified array or not. Basically
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
remove parent element JavaScript
remove parent element JavaScript  How to remove parent element in JavaScript
Searching For Important Issues for Writing Articles
Searching For Important Issues for Writing Articles       People all over the world like to read... issues out of scrap. Whenever you are searching for a key issue, analyze the industry
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
JQuery-Disabling element in JQuery.
JQuery-Disabling element in JQuery.   How can we enable/disable an element in JQuery
ArrayList
). An ArrayList has methods for inserting, deleting, and searching... are implemented with an array, and when that array is full and an additional element..., and next(), which returns the next element as an Object, which must be downcast
Searching a tex on a table in java! - Java Beginners
Searching a tex on a table in java!  i have a table which contains several data. using a textfield the item hat i search must be selected and will show with using a scroll bar.... how should i do this.... tnx for the response
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
XML root element
XML root element  Hi..... please tell me about that Can a root element type be explicitly declared in the DTD? 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
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
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
remove element from stack
remove element from stack  Hi there I'm looking around on the internet how to remove names from stack by asking user how many names they want to remove and if the number they enter is bigger than the stack number then show
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
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

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.