pagination problem
Thanks sir for posting the answer.But i have 2 problems
1.i have created one employee class in that i use getter and setter methods,i created another class in that i wrote the methods like modify(),delete(),insert() methods using or importing employee class.finally i created a class using servlets in that i called a these methods,to call this servlet i created a html file i wrote one form to call this servlet.
In this servlet i used doget()method in that i created a 2 forms to call a methods like insert() and modify(),my problem is that where i have to put & how to do pagination?
my code as follows.
//EmployeeCheckServlet.java
package com.antares.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.antares.To.Employee;
import com.antares.service.EmployeeDataBase;
@SuppressWarnings("serial")
public class EmployeeCheckServlet extends HttpServlet {
Employee employeeDTO = null;
ServletContext context;
@Override
public void init() throws ServletException {
System.out.println(" Inside EmployeeCheckServlet.init()");
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(" Inside EmployeeCheckServlet.doGet()");
String strServletPath = request.getServletPath();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("************ Path is " + strServletPath);
try {
String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@twtesting:1550:orcl1";
String user = "supplyco";
String pwd = "supplyco";
try {
Class.forName(className);
connection = DriverManager.getConnection(url, user, pwd);
statement = connection.createStatement();
System.out.println("Connection established");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
if (strServletPath.equalsIgnoreCase("/new.do")) {
out.println("<html>");
out
.println("<body bgcolor='pink'><h3>Hello User Enter The Following.....</h3>");
out.println("<form action='./newuser.do' method='get'>");
out.println("<table>");
out
.println("<tr><td>Enter Your First Name</td><td><input type='text' name='fstname'></td></tr>");
out
.println("<tr><td>Enter Your Last Name</td><td><input type='text' name='lstname'></td></tr>");
out
.println("<tr><td>Enter Your Address</td><td><input type='text' name='add'></td></tr>");
out
.println("<tr><td>Enter Your Phone Number</td><td><input type='text' name='phno'></td></tr>");
out
.println("<tr><td>Enter Your Email ID</td><td><input type='text' name='emailid'></td></tr>");
out
.println("<tr><td>Enter Your Status</td><td><input type='text' name='status'></td></tr>");
out
.println("<tr><td colspan='1' align='center'><input type='submit' value='Click Here To Submit'></td></tr>");
out.println("</table></form></body>");
out.println("</html>");
} else if (strServletPath.equalsIgnoreCase("/newuser.do")) {
String strFirstName = request.getParameter("fstname");
String strLastName = request.getParameter("lstname");
String strAddress = request.getParameter("add");
String strPhoneNumber = request.getParameter("phno");
String strEmailId = request.getParameter("emailid");
String strStatus = request.getParameter("status");
int intPhoneNumber = Integer.parseInt(strPhoneNumber);
System.out.println("**** FIRST NAME IS ****"+strFirstName);
employeeDTO = new Employee();
EmployeeDataBase empDB = new EmployeeDataBase();
employeeDTO = empDB.insertEmployeeDetails(strFirstName,
strLastName, strAddress, intPhoneNumber, strEmailId,
strStatus);
ResultSet resultSet = null;
boolean blnIsValid = false;
try {
resultSet = statement
.executeQuery("SELECT * FROM EMPLOYEE");
while (resultSet.next()) {
if ((strFirstName.equalsIgnoreCase(resultSet.getString("EMP_FIRSTNAME")))
&& (strLastName.equalsIgnoreCase(resultSet.getString("EMP_LASTNAME")))
&& (strAddress.equalsIgnoreCase(resultSet.getString("EMP_ADDRESS")))
&& (strPhoneNumber.equalsIgnoreCase(resultSet.getString("EMP_PHONENO")))
&& (strEmailId.equalsIgnoreCase(resultSet.getString("EMP_EMAILID")))
&& (strStatus.equalsIgnoreCase(resultSet.getString("EMP_STATUS")))) {
blnIsValid = true;
break;
}
}
if (blnIsValid) {
out.println("<html>");
out.println("<body bgcolor='pink'>");
out.println("<h3>Your Personal Details As Follows :</h3>");
out.println("<br>Your Emp_id :"+ resultSet.getInt("EMP_ID"));
out.print("<br>Your First Name :" + strFirstName);
out.print("<br>Your Last Name :" + strLastName);
out.print("<br>Your Address :" + strAddress);
out.print("<br>Your Phone Number :" + strPhoneNumber);
out.print("<br>Your Email ID :" + strEmailId);
out.print("<br>Your Status :" + strStatus);
out.print("<br><br><br> <a href='./empsumm.do'>Click Here To Go Employee Summery </a>");
out.println("</body></html>");
}
else {
out.println("<html>");
out.println("<body bgcolor='babypink'>");
out.println("<h3>Sorry User Your Details Arenot Correct Please Enter Once Again!!!!!!!!</h3>");
out.println("</body></html>");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
else if (strServletPath.equalsIgnoreCase("/empsumm.do")) {
out.println("<html>");
out.println("<body bgcolor='wheat'><center><h3>Following Are List Of Employees.....</h3></center>");
out.println("<form action='./modify.do' method='get'>");
out.println("<center><table border='2'cellpadding='7' cellspacing='1'>");
out.println("<tr><td>Select</td><td>EmpId</td><td>First Name</td><td>Last Name</td><td>Address</td><td>Phone Number</td><td>EmailID</td><td>Status</td>");
EmployeeDataBase empDB = new EmployeeDataBase();
ArrayList<Employee> arlEmp = empDB.findAllEmployees();
Iterator<Employee> iter = arlEmp.iterator();
int intEmpId = 0, intEmpPhoneNo = 0;
String strFstName = null, strLstName = null, strEmpAdd = null, strEmailId = null, strStatus = null;
while (iter.hasNext()) {
Employee empDTO = (Employee) iter.next();
intEmpId = empDTO.getIntEmpid();
strFstName =empDTO.getEmp_fristName();
strLstName=empDTO.getEmp_lastName();
strEmpAdd = empDTO.getEmp_address();
intEmpPhoneNo = empDTO.getEmp_phone();
strEmailId = empDTO.getEmp_email();
strStatus = empDTO.getEmp_status();
System.out.println();
out.println("<tr>");
out.println("<td><input type='radio' value='"+intEmpId+"' name='empid'></td>");
out.println("<td>" + intEmpId + "</td>");
out.println(" <td>" + strFstName + "</td>");
out.println(" <td>" + strLstName + "</td>");
out.println(" <td>" + strEmpAdd + "</td>");
out.println(" <td> " + intEmpPhoneNo + "</td>");
out.println(" <td>" + strEmailId + "</td>");
out.println("<td>" + strStatus + "</td>");
out.println("</tr>");
}
out.println("</table><br><br><input align='right' type='submit' value='To Modify'></form></center><form action='./new.do' method='get'><center><input type='submit' value='New User'></center></form>");
out.println("<form action='./delete.do' method='get'><center><input type='hidden' value="+intEmpId+" name='empid'>");
out.println("<input align='right' type='submit' value='To Delete'></center></form>");
out.println("</body>");
out.println("</html>");
} else if (strServletPath.equalsIgnoreCase("/modify.do")) {
String strEmpId = request.getParameter("empid");
System.out.println("******* empid Is ******" + strEmpId);
int intempid=Integer.parseInt(strEmpId);
String strFstName = null;
String strLstName = null ;
String strEmpAdd = null ;
int intEmpPhoneNo = 0 ;
String strEmailId = null ;
String strStatus = null ;
ResultSet resultSet=null;
resultSet=statement.executeQuery("SELECT * FROM EMPLOYEE WHERE EMP_ID="+strEmpId);
while(resultSet.next())
{
//intEmpId1 = resultSet.getInt("EMP_ID");
strFstName = resultSet.getString("EMP_FIRSTNAME");
strLstName = resultSet.getString("EMP_LASTNAME");
strEmpAdd = resultSet.getString("EMP_ADDRESS");
intEmpPhoneNo = resultSet.getInt("EMP_PHONENO");
strEmailId = resultSet.getString("EMP_EMAILID");
strStatus = resultSet.getString("EMP_STATUS");
}
out.println("<html>");
out.println("<body bgcolor='orange'><h3>To Modify Anything Enter The Following</h3>");
out.println("<form action='./modifytable.do' method='get'>");
out.println("<table>");
out.println("<tr><td>EmpId</td> <td><input type='text' value="+strEmpId+" name='empid'></td> </tr>");
out.println("<tr><td>Enter First Name</td> <td><input type='text' value="+strFstName+" name='fstname'></td> </tr>");
out.println("<tr><td>Enter Last Name</td> <td><input type='text' value="+strLstName+" name='lstname'></td> </tr>");
out.println("<tr><td>Enter Address</td> <td><input type='text' value="+strEmpAdd+" name='address'></td> </tr>");
out.println("<tr><td>Enter PhoneNumber</td> <td><input type='text' value="+intEmpPhoneNo+" name='phone'></td> </tr>");
out.println("<tr><td>Enter EmailId</td> <td><input type='te"+ "xt' value="+strEmailId+" name" + "='emailid'></td> </tr>");
out.println("<tr><td>Enter Status</td> <td><input type='text' value="+strStatus+" name='status'></td> </tr>");
out.println("<tr><td colspan='1' align='center'><input type='submit' value='Click Here To Submit'></td></tr></table></form>");
out.print("<br><br> <a href='./empsumm.do'>Click Here To Go Employee Summery </a>");
out.println("<br><br><h3>DO You Want To Delete A Record.</h3><form action='./delete.do' method='get'><center><input type='hidden' value='"+intempid+"' name='empid'><input align='right' type='submit' value='To Delete'></center></form>");
out.println("</body><html>");
} else if (strServletPath.equalsIgnoreCase("/modifytable.do")) {
String strEmpId=request.getParameter("empid");
System.out.println("******VALUE OF EMPID IS*****" + strEmpId);
String strFirstName = request.getParameter("fstname");
String strLastName = request.getParameter("lstname");
String strAddress = request.getParameter("address");
String strPhoneNumber = request.getParameter("phone");
String strEmailId = request.getParameter("emailid");
String strStatus = request.getParameter("status");
int intPhoneNumber = Integer.parseInt(strPhoneNumber);
employeeDTO = new Employee();
EmployeeDataBase empDB = new EmployeeDataBase();
int intEmpId = Integer.parseInt(strEmpId);
System.out.println("******VALUE OF EMPID IS*****" + intEmpId);
employeeDTO = empDB.modifyEmployee(intEmpId, strFirstName,
strLastName, strAddress, intPhoneNumber, strEmailId,
strStatus);
boolean blnIsValid = false;
resultSet = statement.executeQuery("SELECT * FROM EMPLOYEE");
while (resultSet.next()) {
if ((strFirstName.equalsIgnoreCase(resultSet
.getString("EMP_FIRSTNAME")))
&& (strLastName.equalsIgnoreCase(resultSet
.getString("EMP_LASTNAME")))) {
blnIsValid = true;
break;
}
}
if (blnIsValid) {
out.println("<html>");
out.println("<body bgcolor='pink'>");
out.println("<h3>After Modification Your Personal Details As Follows :</h3>");
out.println("<br>Your Emp_id :"+ resultSet.getInt("EMP_ID"));
out.print("<br>Your First Name :" + strFirstName);
out.print("<br>Your Last Name :" + strLastName);
out.print("<br>Your Address :" + strAddress);
out.print("<br>Your Phone Number :" + strPhoneNumber);
out.print("<br>Your Email ID :" + strEmailId);
out.print("<br>Your Status :" + strStatus);
out.println("<br><br> <a href='./html/login.html'> Click Here To Go Home Page </a>");
out.println("<br><br> <a href='./empsumm.do'>Click Here To Go Employee Summery </a>");
out.println("</body></html>");
}
}
else if (strServletPath.equalsIgnoreCase("/delete.do")) {
String strEmpId = request.getParameter("empid");
System.out.println("******* empid in delete method Is ******" + strEmpId);
out.println("<html>");
out.println("<body bgcolor='skyblue'><form action='./rowdelete.do' method='get'>");
out.println("<h3>Are You Sure Want To Delete The Record....Where Emp_Id="+strEmpId+"</h3><input type='hidden' value='"+strEmpId+"' name='empid'><input type='submit' value='Yes'>");
out.println("<br><br> <a href='./empsumm.do'>Are Else Click Here To Go Employee Summery </a>");
out.println("</body></html>");
}
else if (strServletPath.equalsIgnoreCase("/rowdelete.do")) {
String strEmpId=request.getParameter("empid");
int intEmpId = Integer.parseInt(strEmpId);
EmployeeDataBase empDB = new EmployeeDataBase();
empDB.deleteRow(intEmpId);
out.println("<html>");
out.println("<body bgcolor='+9999-999123blue'><form action='rowdelete.do' method='get'>");
out.println("<h3>The Employee Details Completely Deleted!!!!!!!!</h3>");
out.println("<br><br> <a href='./html/login.html'> Click Here To Go Home Page </a>");
out.println("</body></html>");
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally
{
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public void finalize() throws Throwable {
if(connection!=null)
{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//EmployeeDataBase.java
package com.antares.service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.antares.To.Employee;
import com.antares.Util.DBUtil;
public class EmployeeDataBase {
Connection connection = null;
Statement statement = null;
public EmployeeDataBase() {
try {
connection = DBUtil.getOracleConnection();
statement = connection.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Employee findEmployeeDetails(int pEmp_id) {
Employee dto1=null;
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery("SELECT * FROM EMPLOYEE WHERE EMP_ID="+ pEmp_id);
while (resultSet.next())
{
dto1 = new Employee();
dto1.setIntEmpid(resultSet.getInt(1));
dto1.setEmp_fristName(resultSet.getString(2));
dto1.setEmp_lastName(resultSet.getString(3));
dto1.setEmp_address(resultSet.getString(4));
dto1.setEmp_phone(resultSet.getInt(5));
dto1.setEmp_email(resultSet.getString(6));
dto1.setEmp_status(resultSet.getString(7));
}
} catch (SQLException e) {
e.printStackTrace();
}
return dto1;
}
public ArrayList<Employee> findAllEmployees() {
ArrayList<Employee> arlEmployee=new ArrayList<Employee>();
ResultSet resultSet = null;
Employee dto=null;
try {
String strSql = "select * from employee";
resultSet = statement.executeQuery(strSql);
while(resultSet.next())
{
dto=new Employee();
dto.setIntEmpid(resultSet.getInt("EMP_ID"));
dto.setEmp_fristName(resultSet.getString("EMP_FIRSTNAME"));
dto.setEmp_lastName(resultSet.getString("EMP_LASTNAME"));
dto.setEmp_address(resultSet.getString("EMP_ADDRESS"));
dto.setEmp_phone(resultSet.getInt("EMP_PHONENO"));
dto.setEmp_email(resultSet.getString("EMP_EMAILID"));
dto.setEmp_status(resultSet.getString("EMP_STATUS"));
arlEmployee.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
}
return arlEmployee;
}
public Employee insertEmployeeDetails( String pstrFstname,
String pstrLstname, String pstrAddress, int pintPhoneno,
String pstrEmailid, String pstrStatus) {
ResultSet resultSet = null;
Employee employeeDTo=null;
try {
statement.executeUpdate("insert into employee values(employee_seq.nextval,'" + pstrFstname + "','" + pstrLstname
+ "','" + pstrAddress + "'," + pintPhoneno + ",'"
+ pstrEmailid + "','" + pstrStatus + "')");
resultSet =statement.executeQuery("SELECT * FROM EMPLOYEE");
while(resultSet.next())
{
employeeDTo=new Employee();
employeeDTo.setIntEmpid(resultSet.getInt("EMP_ID"));
employeeDTo.setEmp_fristName(resultSet.getString("EMP_FIRSTNAME"));
employeeDTo.setEmp_lastName(resultSet.getString("EMP_LASTNAME"));
employeeDTo.setEmp_address(resultSet.getString("EMP_ADDRESS"));
employeeDTo.setEmp_phone(resultSet.getInt("EMP_PHONENO"));
employeeDTo.setEmp_email(resultSet.getString("EMP_EMAILID"));
employeeDTo.setEmp_status(resultSet.getString("EMP_STATUS"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeeDTo;
}
public Employee modifyEmployeePhoneNumber(int pintEmpid,int pintPhoneno) {
Employee employeeDTo = null;
ResultSet resultSet=null;
try {
statement.executeUpdate("update employee set emp_phoneno="+pintPhoneno+"where emp_id="+pintEmpid);
resultSet =statement.executeQuery("SELECT * FROM EMPLOYEE");
while(resultSet.next())
{
employeeDTo=new Employee();
employeeDTo.setIntEmpid(resultSet.getInt("EMP_ID"));
employeeDTo.setEmp_fristName(resultSet.getString("EMP_FIRSTNAME"));
employeeDTo.setEmp_lastName(resultSet.getString("EMP_LASTNAME"));
employeeDTo.setEmp_address(resultSet.getString("EMP_ADDRESS"));
employeeDTo.setEmp_phone(resultSet.getInt("EMP_PHONENO"));
employeeDTo.setEmp_email(resultSet.getString("EMP_EMAILID"));
employeeDTo.setEmp_status(resultSet.getString("EMP_STATUS"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeeDTo;
}
public Employee modifyEmployeeAddress(int pintEmpid,String strAddress) {
Employee employeeDTo = null;
ResultSet resultSet=null;
try {
statement.executeUpdate("update employee set EMP_ADDRESS='"+strAddress+"' where emp_id="+pintEmpid);
resultSet =statement.executeQuery("SELECT * FROM EMPLOYEE");
while(resultSet.next())
{
employeeDTo=new Employee();
employeeDTo.setIntEmpid(resultSet.getInt("EMP_ID"));
employeeDTo.setEmp_fristName(resultSet.getString("EMP_FIRSTNAME"));
employeeDTo.setEmp_lastName(resultSet.getString("EMP_LASTNAME"));
employeeDTo.setEmp_address(resultSet.getString("EMP_ADDRESS"));
employeeDTo.setEmp_phone(resultSet.getInt("EMP_PHONENO"));
employeeDTo.setEmp_email(resultSet.getString("EMP_EMAILID"));
employeeDTo.setEmp_status(resultSet.getString("EMP_STATUS"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeeDTo;
}
public Employee modifyEmployeeStatus(int pintEmpid,String pstrStatus) {
Employee employeeDTo = null;
ResultSet resultSet=null;
try {
statement.executeUpdate("update employee set EMP_STATUS='"+pstrStatus+"' where emp_id="+pintEmpid);
resultSet =statement.executeQuery("SELECT * FROM EMPLOYEE");
while(resultSet.next())
{
employeeDTo=new Employee();
employeeDTo.setIntEmpid(resultSet.getInt("EMP_ID"));
employeeDTo.setEmp_fristName(resultSet.getString("EMP_FIRSTNAME"));
employeeDTo.setEmp_lastName(resultSet.getString("EMP_LASTNAME"));
employeeDTo.setEmp_address(resultSet.getString("EMP_ADDRESS"));
employeeDTo.setEmp_phone(resultSet.getInt("EMP_PHONENO"));
employeeDTo.setEmp_email(resultSet.getString("EMP_EMAILID"));
employeeDTo.setEmp_status(resultSet.getString("EMP_STATUS"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeeDTo;
}
public Employee modifyEmployee(int pintEmpid,String pstrFstName,String pstrLstName,String pstrAddress,int pintPhone,String pstrEmailid,String pstrStatus) {
Employee employeeDTo = null;
ResultSet resultSet=null;
try {
statement.executeUpdate("update employee set EMP_FIRSTNAME='"+pstrFstName+"',EMP_LASTNAME='"+pstrLstName+"',EMP_ADDRESS='"+pstrAddress+"',EMP_PHONENO="+pintPhone+",EMP_EMAILID='"+pstrEmailid+"',EMP_STATUS='"+pstrStatus+"' where emp_id="+pintEmpid);
resultSet =statement.executeQuery("SELECT * FROM EMPLOYEE");
while(resultSet.next())
{
employeeDTo=new Employee();
employeeDTo.setIntEmpid(resultSet.getInt("EMP_ID"));
employeeDTo.setEmp_fristName(resultSet.getString("EMP_FIRSTNAME"));
employeeDTo.setEmp_lastName(resultSet.getString("EMP_LASTNAME"));
employeeDTo.setEmp_address(resultSet.getString("EMP_ADDRESS"));
employeeDTo.setEmp_phone(resultSet.getInt("EMP_PHONENO"));
employeeDTo.setEmp_email(resultSet.getString("EMP_EMAILID"));
employeeDTo.setEmp_status(resultSet.getString("EMP_STATUS"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return employeeDTo;
}
public void deleteRow(int pintEmpid)
{
@SuppressWarnings("unused")
ResultSet resultSet=null;
try {
statement.executeUpdate("delete employee where emp_id="+pintEmpid);
resultSet =statement.executeQuery("SELECT * FROM EMPLOYEE");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//login.html
<html>
<body bgcolor="blue">
<marquee bgcolor="violet">
<h2 dir="rtl">** Hello Welcome To **</h2>
</marquee>
<br>
<br>
<h1 align="center">Antares Systems Limited</h1>
<br>
<br>
<form action="../empsumm.do" method="get">
<center >
<input type="submit"value="Click To See Employee Summary ">
</center>
</form>
</body>
</html>
//Employee.java
package com.antares.To;
public class Employee {
int intEmpid;
String emp_fristName;
String emp_lastName;
String emp_address;
int emp_phone;
String emp_email;
String emp_status;
public int getIntEmpid() {
return intEmpid;
}
public void setIntEmpid(int intEmpid) {
this.intEmpid = intEmpid;
}
public String getEmp_fristName() {
return emp_fristName;
}
public void setEmp_fristName(String emp_fristName) {
this.emp_fristName = emp_fristName;
}
public String getEmp_lastName() {
return emp_lastName;
}
public void setEmp_lastName(String emp_lastName) {
this.emp_lastName = emp_lastName;
}
public String getEmp_address() {
return emp_address;
}
public void setEmp_address(String emp_address) {
this.emp_address = emp_address;
}
public int getEmp_phone() {
return emp_phone;
}
public void setEmp_phone(int emp_phone) {
this.emp_phone = emp_phone;
}
public String getEmp_email() {
return emp_email;
}
public void setEmp_email(String emp_email) {
this.emp_email = emp_email;
}
public String getEmp_status() {
return emp_status;
}
public void setEmp_status(String emp_status) {
this.emp_status = emp_status;
}
}
2.please tell me the how to do the delete().i done but it is accessing the last row employeeid ,so how to get the empid of which we are clicking to delete.
I m sending my full code ,please replay me.......
View Answers