JSP and Hibernate CRUD application With Pagination, Sorting and export option using NetBeans IDE And MySQL 5

In this article you will learn how to create a JSP CRUD application with Pagination, Sorting and export option using NetBeans IDE and MySQL 5. This simple application is a Create, Read, Update and Delete application operating on an ‘emp’ table in ‘test’ database in MySQL Database Server.

Software Used

  1. JDK 8u25
  2. NetBeans IDE 8.02
  3. MySQL 5.*(or XAMPP)
  4. MySQL Connector 5.*
  5. Hibernate 4.3.** (Bundled with NetBeans)
  6. Display Tag Library(For pagination ,sorting and export)

Steps:

  1. Install JDK8 or JDK7 if not installed.
  2. Install NetBeans and associated ApacheTomcat Server.
  3. Install MySQL Database server or XAMPP (for easy MySQL management).

After Installing NetBeans click the services tab on the left.Expand Database node.Expand Drivers node. Right click MySQL(Connector/Jdriver) then connect.Put test as the database. As shown below. Put password if you have given password at the time of installation of MySQL database server. For XAMPP no password is required. Then test connection. If successful click finish button.

finish

Create ‘Emp’ Table using below script in MySQL TEST Database,

  1. CREATE TABLE `Emp` (  
  2.   
  3. `empno` int(10) unsigned NOT NULL auto_increment,  
  4.   
  5. `ename` varchar(45) NOT NULL,  
  6.   
  7. `sal` int(15) NOT NULL,  
  8.   
  9. `dob` date NOT NULL,  
  10.   
  11. PRIMARY KEY (`empno`)  
  12.   
  13. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;  

Project Structure:

Structure

Creating Project JSPHibernate4EmployeeCRUD

File, New Project, Categories, Choose JavaWeb, then choose WebApplication, Click Next and Give Project Name JSPHibernate4EmployeeCRUD, then click Next and choose Framework Hibernate, then click Finish

Download and add the following libraries(JAR) one by one by right clicking the libraries folder in project window then-Add JAR/Folder.

  1. MySQL-Connector.java-5.1.35-bin.jar
  2. displaytag-1.2.jar
  3. displaytag-export-poi-1.2.jar
  4. displaytag-portlet-1.2.jar
  5. commons-beanutilis-1.8.0.jar
  6. commons-collections-3.1.jar
  7. commons-lang-2.4.jar

Creating Packages and Classes

Right click SourcePackages folder and create three packages,

  1. com.dao- This would contain DAO (Data Access Object) class empDao.java.
  2. com.model.pojo- This would contain entity class Emp.java.
  3. com.util- This will contain HibernateUtil.java class

Following Files would be created using Netbeans,

  1. hibernate.cfg.xml File-Automatically generated.
  2. Reverse Engineering File-hibernate.reveng.xml.
  3. Entity(POJO) File-Emp.java(POJO stands for Plain Old Java Objects).
  4. DataAccessObject(DAO) File-àempDao.java.
  5. HibernateUtil.java File.
  6. web.xml (Automatically generated)
  7. index.jsp (Add employee record and displays all employee records)
  8. deleteEmp.jsp (Displays employee record before deleting)
  9. updateEmp.jsp (Displays employee record for updating)

1.hibernate.cfg.xml

It is generated automatically when connected to test database of MySQL.

By Netbeans Services Tab àDatabasesàGet connected to test database.

COPY AND PASTE CODE OF THE FILE GIVEN BELOW WHOSE CODE IS NOT GENERATED

2. Creating Reverse Engineering File-àhibernate.reveng.xml

Reverse

Right Click default package in the Source Package, new, choose Hibernate Reverse Engineering Wizard, click next, then choose emp table, Add, then click finish.

Code

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">  
  4.   
  5. <hibernate-reverse-engineering>  
  6.   
  7.     <schema-selection match-catalog="test" />  
  8.   
  9.     <table-filter match-name="emp" />  
  10.   
  11. </hibernate-reverse-engineering>  

3. Creating Annotation Based Entity (pojo) Class File Emp.java

Emp.java Class File

Important:To create this file MySQL database test must be connected through NetBeans.

Right click com.model package--new-Hibernate Mappling Files and pojos from database, then click Finish

Connect to database, then the following window opens . Check EJB3.0 annotation box, uncheck Hibernate XML Mapping check box. Choose package com.model.pojo.

Annotation Based Entity

Emp.java Class File

  1. package com.model.pojo;  
  2.   
  3. // Generated Mar 3, 2016 3:30:15 PM by Hibernate Tools 4.3.1  
  4.   
  5. import java.util.Date;  
  6.   
  7. import javax.persistence.Column;  
  8.   
  9. import javax.persistence.Entity;  
  10.   
  11. import javax.persistence.GeneratedValue;  
  12.   
  13. import static javax.persistence.GenerationType.IDENTITY;  
  14.   
  15. import javax.persistence.Id;  
  16.   
  17. import javax.persistence.Table;  
  18.   
  19. import javax.persistence.Temporal;  
  20.   
  21. import javax.persistence.TemporalType;  
  22.   
  23. /** 
  24.  
  25. * Emp generated by hbm2java 
  26.  
  27. */  
  28.   
  29. @Entity  
  30.   
  31. @Table(name = "emp"  
  32.   
  33.     , catalog = "test"  
  34.   
  35. )  
  36.   
  37. public class Emp implements java.io.Serializable {  
  38.   
  39.     private Integer empno;  
  40.   
  41.     private String ename;  
  42.   
  43.     private Integer sal;  
  44.   
  45.     private Date dob;  
  46.   
  47.     public Emp() {  
  48.   
  49.     }  
  50.   
  51.     public Emp(String ename, Integer sal, Date dob) {  
  52.   
  53.         this.ename = ename;  
  54.   
  55.         this.sal = sal;  
  56.   
  57.         this.dob = dob;  
  58.   
  59.     }  
  60.   
  61.     @Id @GeneratedValue(strategy = IDENTITY)  
  62.   
  63.     @Column(name = "empno", unique = true, nullable = false)  
  64.   
  65.     public Integer getEmpno() {  
  66.   
  67.         return this.empno;  
  68.   
  69.     }  
  70.   
  71.     public void setEmpno(Integer empno) {  
  72.   
  73.         this.empno = empno;  
  74.   
  75.     }  
  76.   
  77.     @Column(name = "ename", length = 20)  
  78.   
  79.     public String getEname() {  
  80.   
  81.         return this.ename;  
  82.   
  83.     }  
  84.   
  85.     public void setEname(String ename) {  
  86.   
  87.         this.ename = ename;  
  88.   
  89.     }  
  90.   
  91.     @Column(name = "sal")  
  92.   
  93.     public Integer getSal() {  
  94.   
  95.         return this.sal;  
  96.   
  97.     }  
  98.   
  99.     public void setSal(Integer sal) {  
  100.   
  101.         this.sal = sal;  
  102.   
  103.     }  
  104.   
  105.     @Temporal(TemporalType.DATE)  
  106.   
  107.     @Column(name = "dob", length = 10)  
  108.   
  109.     public Date getDob() {  
  110.   
  111.         return this.dob;  
  112.   
  113.     }  
  114.   
  115.     public void setDob(Date dob) {  
  116.   
  117.         this.dob = dob;  
  118.   
  119.     }  
  120.   
  121.     //This method writes the values of contact object with System.out.println(Emp.toString()) code  
  122.   
  123.     @Override  
  124.   
  125.     public String toString() {  
  126.   
  127.         return "Emp"  
  128.   
  129.         +"\n\t EmpNo: " + this.empno  
  130.   
  131.             +  
  132.             "\n\t EmployeeName: " + this.ename  
  133.   
  134.             +  
  135.             "\n\t Salary: " + this.sal  
  136.   
  137.             +  
  138.             "\n\t Date of Birth: " + this.dob;  
  139.   
  140.     }  
  141.   
  142. }  

4. Creating DataAccessObject (DAO) File

empDao.java File

Right click com.dao package, new, JavaClass, Give class name empDao, then click Finish.

empDao.java File CODE:

  1. package com.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import java.util.ArrayList;  
  6.   
  7. import org.hibernate.Query;  
  8.   
  9. import org.hibernate.Transaction;  
  10.   
  11. import org.hibernate.Session;  
  12.   
  13. import com.util.HibernateUtil;  
  14.   
  15. import com.model.pojo.Emp;  
  16.   
  17. /** 
  18.  
  19. * 
  20.  
  21. * @author Raichand 
  22.  
  23. */  
  24.   
  25. public class empDao {  
  26.   
  27.     private Emp emp;  
  28.   
  29.     private Emp newemp;  
  30.   
  31.     private List < Emp > DaoAllEmp;  
  32.   
  33.     private List < Emp > DaoSearchEmpList;  
  34.   
  35.     //Session session;  
  36.   
  37.     public List < Emp > getAllEmployee() {  
  38.   
  39.         Session session = HibernateUtil.getSessionFactory().openSession();  
  40.   
  41.         try {  
  42.   
  43.             session.beginTransaction();  
  44.   
  45.             DaoAllEmp = session.createCriteria(Emp.class).list();  
  46.   
  47.             int count = DaoAllEmp.size();  
  48.   
  49.             System.out.println("No of Record From Dao: " + count);  
  50.   
  51.   
  52.             session.getTransaction().commit();  
  53.   
  54.         } catch (Exception e) {  
  55.   
  56.             e.printStackTrace();  
  57.   
  58.             // ses.getTransaction().rollback();  
  59.   
  60.         }  
  61.   
  62.         session.close();  
  63.   
  64.         return DaoAllEmp;  
  65.   
  66.     }  
  67.   
  68.     public List < Emp > SearchByRecordNo(String RecordNo) {  
  69.   
  70.         Session session = HibernateUtil.getSessionFactory().openSession();  
  71.   
  72.         List < Emp > daoSearchList = new ArrayList < > ();  
  73.   
  74.         // List<Item> daoSearchList =null;  
  75.   
  76.         try {  
  77.   
  78.             session.beginTransaction();  
  79.   
  80.             Query qu = session.createQuery("From Emp E where E.recordno =:recordNo"); //User is the entity not the table  
  81.   
  82.             daoSearchList = qu.list();  
  83.   
  84.             int count = daoSearchList.size();  
  85.   
  86.             session.getTransaction().commit();  
  87.   
  88.         } catch (Exception e) {  
  89.   
  90.             e.printStackTrace();  
  91.   
  92.             session.getTransaction().rollback();  
  93.   
  94.   
  95.         } finally {  
  96.   
  97.             session.close();  
  98.   
  99.         }  
  100.   
  101.         return daoSearchList;  
  102.   
  103.     }  
  104.   
  105.     public void add(Emp emp) {  
  106.   
  107.         Transaction trans = null;  
  108.   
  109.         Session session = HibernateUtil.getSessionFactory().openSession();  
  110.   
  111.         try {  
  112.   
  113.             String name = emp.getEname();  
  114.   
  115.             System.out.println("Employee Name:" + name);  
  116.   
  117.             // begin a transaction  
  118.   
  119.             session.beginTransaction();  
  120.   
  121.             session.persist(emp);  
  122.   
  123.             session.getTransaction().commit();  
  124.   
  125.             System.out.println("New Employee saved, EmpNo: " +  
  126.   
  127.                 emp.getEname());  
  128.   
  129.         } catch (RuntimeException e) {  
  130.   
  131.             if (trans != null) {  
  132.   
  133.                 trans.rollback();  
  134.   
  135.             }  
  136.   
  137.             e.printStackTrace();  
  138.   
  139.         } finally {  
  140.   
  141.             session.flush();  
  142.   
  143.             session.close();  
  144.   
  145.         }  
  146.   
  147.   
  148.     }  
  149.   
  150.   
  151.     public void delete(Emp emp) {  
  152.   
  153.         Transaction trans = null;  
  154.   
  155.         Session session = HibernateUtil.getSessionFactory().openSession();  
  156.   
  157.         try {  
  158.   
  159.             String EmpName = (emp.getEname());  
  160.   
  161.             System.out.println("Emp deleted, EmpName: " + EmpName);  
  162.   
  163.             // begin a transaction  
  164.   
  165.             session.beginTransaction()  
  166.   
  167.             session.delete(emp);  
  168.   
  169.             session.getTransaction().commit();  
  170.   
  171.             System.out.println("Emp Deleted, : " + emp.toString());  
  172.   
  173.         } catch (RuntimeException e) {  
  174.   
  175.             if (trans != null) {  
  176.   
  177.                 trans.rollback();  
  178.   
  179.             }  
  180.   
  181.             e.printStackTrace();  
  182.   
  183.         } finally {  
  184.   
  185.             session.flush();  
  186.   
  187.             session.close();  
  188.   
  189.         }  
  190.   
  191.   
  192.     }  
  193.   
  194.   
  195.     public void update(Emp emp) {  
  196.   
  197.         Transaction trans = null;  
  198.   
  199.         Session session = HibernateUtil.getSessionFactory().openSession();  
  200.   
  201.         try {  
  202.   
  203.             String EmpName = (emp.getEname());  
  204.   
  205.             System.out.println("Emp updated, EmpName: " + EmpName);  
  206.   
  207.             // begin a transaction  
  208.   
  209.             session.beginTransaction();  
  210.   
  211.             session.update(emp);  
  212.   
  213.             session.getTransaction().commit()  
  214.   
  215.         } catch (RuntimeException e) {  
  216.   
  217.             if (trans != null) {  
  218.   
  219.                 trans.rollback();  
  220.   
  221.             }  
  222.   
  223.             e.printStackTrace();  
  224.   
  225.         } finally {  
  226.   
  227.             session.flush();  
  228.   
  229.             session.close();  
  230.   
  231.         }  
  232.   
  233.     }  
  234.   
  235.     public Emp getEmpbyEmpNo(int empno) {  
  236.   
  237.         Session session = HibernateUtil.getSessionFactory().openSession();  
  238.   
  239.         session.beginTransaction();  
  240.   
  241.         emp = (Emp) session.load(Emp.class, empno);  
  242.   
  243.         System.out.println(emp.toString());  
  244.   
  245.         session.getTransaction().commit();  
  246.   
  247.         session.close();  
  248.   
  249.         return emp;  
  250.   
  251.     }  
  252.   
  253. }  

5.HibernateUtil.java File

Right click com.util folder, new, javaclass, Class name, then give name HibernateUtil, then click Finish.

HibernateUtil.java CODE:

  1. import org.hibernate.SessionFactory;  
  2.   
  3. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  4.   
  5. import org.hibernate.cfg.Configuration;  
  6.   
  7. import org.hibernate.service.ServiceRegistry;  
  8.   
  9. public class HibernateUtil  
  10.   
  11. {  
  12.   
  13.     private static SessionFactory sessionFactory = buildSessionFactory();  
  14.   
  15.     private static SessionFactory buildSessionFactory()  
  16.   
  17.     {  
  18.   
  19.         try  
  20.   
  21.         {  
  22.   
  23.             if (sessionFactory == null) {  
  24.   
  25.                 // loads configuration and mappings  
  26.   
  27.                 Configuration configuration = new Configuration().configure();  
  28.   
  29.                 ServiceRegistry serviceRegistry  
  30.   
  31.                     = new StandardServiceRegistryBuilder()  
  32.   
  33.                 .applySettings(configuration.getProperties()).build();  
  34.   
  35.                 // builds a session factory from the service registry  
  36.   
  37.                 sessionFactory = configuration.buildSessionFactory(serviceRegistry);  
  38.   
  39.             }  
  40.   
  41.             return sessionFactory;  
  42.   
  43.         } catch (Throwable ex)  
  44.   
  45.         {  
  46.   
  47.             System.err.println("Initial SessionFactory creation failed." + ex);  
  48.   
  49.             throw new ExceptionInInitializerError(ex);  
  50.   
  51.         }  
  52.   
  53.     }  
  54.   
  55.     public static SessionFactory getSessionFactory()  
  56.   
  57.     {  
  58.   
  59.         return sessionFactory;  
  60.   
  61.     }  
  62.   
  63.     public static void closeSession()  
  64.   
  65.     {  
  66.         getSessionFactory().close();  
  67.   
  68.     }  
  69.   
  70. }  

6. web.xml (Automatically generated)

CODE:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  
  4.   
  5. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">  
  6.   
  7.     <session-config>  
  8.   
  9.         <session-timeout>  
  10.   
  11.             30  
  12.   
  13.         </session-timeout>  
  14.   
  15.     </session-config>  
  16.   
  17.     <welcome-file-list>  
  18.   
  19.         <welcome-file>index.jsp</welcome-file>  
  20.   
  21.     </welcome-file-list>  
  22.   
  23. </web-app>  

6. Creating index.jsp File

Right click WebPages folderàNewàJspàGive name index.jsp then click Finish.

Similarly create deleteEmp.jsp and updateEmp.jsp files.

Index.jsp

7.index.jsp code:

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  2.   
  3.     <%@page import="java.util.ArrayList"%>  
  4.   
  5.         <%@page import="com.model.pojo.Emp"%>  
  6.   
  7.             <%@page import ="java.util.Date"%>  
  8.   
  9.                 <%@page import = "java.text.SimpleDateFormat"%>  
  10.   
  11.                     <%@page import="com.dao.empDao"%>  
  12.   
  13.                         <%@page import="java.util.List"%>  
  14.   
  15.                             <jsp:useBean id="emp" class="com.model.pojo.Emp">  
  16.   
  17.                             </jsp:useBean>  
  18.   
  19.                             <jsp:setProperty property="*" name="emp" />  
  20.   
  21.                             <%@ taglib prefix="display" uri="http://displaytag.sf.net"%>  
  22.   
  23.                                 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  24.   
  25.                                 <html>  
  26.   
  27.                                 <head>  
  28.   
  29.                                     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  30.   
  31.   
  32.                                     <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">  
  33.   
  34.                                     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>  
  35.   
  36.                                     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>  
  37.   
  38.                                     <script type="text/javascript">  
  39.                                         $(document).ready(function() {  
  40.   
  41.                                             $("#datepicker").datepicker({  
  42.   
  43.                                                 showButtonPanel: true,  
  44.   
  45.                                                 showOn: "button",  
  46.   
  47.                                                 buttonText: "Select date",  
  48.   
  49.                                                 dateFormat: 'yy-mm-dd',  
  50.   
  51.                                                 changeMonth: true,  
  52.   
  53.                                                 changeYear: true,  
  54.   
  55.                                                 yearRange: "-90:"  
  56.   
  57.   
  58.                                             });  
  59.   
  60.                                         });  
  61.                                     </script>  
  62.   
  63.                                     <title>Display tag Pagination and Sorting Example in JSP</title>  
  64.   
  65.                                 </head>  
  66.   
  67.   
  68.                                 <%  
  69.   
  70.   
  71. empDao dao = new empDao();  
  72.   
  73. List empList = dao.getAllEmployee();  
  74.   
  75. request.setAttribute("empList", empList);  
  76.   
  77. int count = dao.getAllEmployee().size();  
  78.   
  79. System.out.println("No of Record: " + count);  
  80.   
  81. String name=request.getParameter("ename");  
  82.   
  83. String sd = request.getParameter("sd");  
  84.   
  85. System.out.println(sd);  
  86.   
  87. System.out.println(name);  
  88.   
  89. if(name != null)  
  90.   
  91. {  
  92.   
  93.   
  94. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  95.   
  96. Date dob = sdf.parse(sd);  
  97.   
  98. emp.setDob(dob);  
  99.   
  100. dao.add(emp);  
  101.   
  102. empList = dao.getAllEmployee();  
  103.   
  104. request.setAttribute("empList", empList);  
  105.   
  106. }  
  107.   
  108. %>  
  109.   
  110.   
  111.                                     <body>  
  112.   
  113.                                         <form name="f1" action="index.jsp" method="post">  
  114.   
  115.                                             <table width="471" border="1">  
  116.   
  117.   
  118.                                                 <tr>  
  119.   
  120.                                                     <th>Employee Name </th>  
  121.   
  122.                                                     <td><input name="ename" type="text"></td>  
  123.   
  124.                                                 </tr>  
  125.   
  126.                                                 <tr>  
  127.   
  128.                                                     <th>Salary </th>  
  129.   
  130.                                                     <td><input name="sal" type="text"></td>  
  131.   
  132.                                                 </tr>  
  133.   
  134.                                                 <tr>  
  135.   
  136.                                                     <th>Date of Birth</th>  
  137.   
  138.                                                     <td><input name="sd" type="text" id="datepicker"></td>  
  139.   
  140.                                                 </tr>  
  141.   
  142.                                                 <tr>  
  143.   
  144.                                                     <th colspan="2"><input type="submit" onclick=u pdate(EmpTable) name="s1" value="Add">  
  145.   
  146.                                                     </th>  
  147.   
  148.                                                 </tr>  
  149.   
  150.                                             </table>  
  151.   
  152.                                         </form>  
  153.   
  154.   
  155.                                         <display:table id="EmpTable" name="empList" class="dataTable" export="true" pagesize="10" cellpadding="5px;" uid="row" cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">  
  156.   
  157.                                             <display:column property="ename" title="Name" sortable="true" />  
  158.   
  159.                                             <display:column property="sal" title="Salary" />  
  160.   
  161.                                             <display:column property="dob" format="{0,date,dd-MMM-yyyy}" title="BirthDate" />  
  162.   
  163.                                             <display:column property="empno" format="Edit" href="updateEmp.jsp" paramId="empno" paramProperty="empno" title="Action" />  
  164.   
  165.                                             <display:column property="empno" format="Delete" href="deleteEmp.jsp" paramId="empno" paramProperty="empno" title="Action" />  
  166.   
  167.                                             <display:setProperty name="export.excel.filename" value="EmployeeDetails.xls" />  
  168.   
  169.                                             <display:setProperty name="export.pdf.filename" value="EmployeeDetails.pdf" />  
  170.   
  171.                                             <display:setProperty name="export.csv.filename" value="EmployeeDetails.csv" />  
  172.   
  173.                                         </display:table>  
  174.   
  175.                                     </body>  
  176.   
  177.                                 </html>  

8.DeleteEmp.jsp file code:

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  2.   
  3.     <%@page import="java.util.ArrayList"%>  
  4.   
  5.         <%@page import="com.model.pojo.Emp"%>  
  6.   
  7.             <%@page import ="java.util.Date"%>  
  8.   
  9.                 <%@page import = "java.text.SimpleDateFormat"%>  
  10.   
  11.                     <%@page import="com.dao.empDao"%>  
  12.   
  13.                         <%@page import="java.util.List"%>  
  14.   
  15.                             <jsp:useBean id="emp" class="com.model.pojo.Emp">  
  16.   
  17.                             </jsp:useBean>  
  18.   
  19.                             <%@ taglib prefix="display" uri="http://displaytag.sf.net"%>  
  20.   
  21.                                 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  22.   
  23.                                 <html>  
  24.   
  25.                                 <head>  
  26.   
  27.                                     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  28.   
  29.                                     <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">  
  30.   
  31.                                     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>  
  32.   
  33.                                     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>  
  34.   
  35.                                     <script type="text/javascript">  
  36.                                         $(document).ready(function() {  
  37.   
  38.                                             $("#datepicker").datepicker({  
  39.   
  40.                                                 showButtonPanel: true,  
  41.   
  42.                                                 showOn: "button",  
  43.   
  44.                                                 buttonText: "Select date",  
  45.   
  46.                                                 dateFormat: 'yy-mm-dd',  
  47.   
  48.                                                 changeMonth: true,  
  49.   
  50.                                                 changeYear: true,  
  51.   
  52.                                                 yearRange: "-90:"  
  53.   
  54.   
  55.                                             });  
  56.   
  57.                                         });  
  58.                                     </script>  
  59.   
  60.                                     <title>Update Employee</title>  
  61.   
  62.                                 </head>  
  63.   
  64.                                 <body>  
  65.   
  66.                                     <%  
  67.   
  68. String sub=request.getParameter("s1");  
  69.   
  70. empDao dao = new empDao();  
  71.   
  72. Emp deleteemp = new Emp();  
  73.   
  74. String num=request.getParameter("empno");  
  75.   
  76. int salary=0;  
  77.   
  78. String name="";  
  79.   
  80. Date dob=null;  
  81.   
  82. if(sub == null){  
  83.   
  84. //num=request.getParameter("empno");  
  85.   
  86. int empno =Integer.parseInt(num);  
  87.   
  88. deleteemp = dao.getEmpbyEmpNo(empno);  
  89.   
  90. salary = deleteemp.getSal();  
  91.   
  92. name = deleteemp.getEname();  
  93.   
  94. //out.println(deleteemp.toString());//Debugging Purpose  
  95.   
  96. dob = deleteemp.getDob();  
  97.   
  98. }  
  99.   
  100. if(sub != null)  
  101.   
  102. {  
  103.   
  104. //deleting record  
  105.   
  106. int eno= Integer.parseInt(request.getParameter("eno"));  
  107.   
  108. deleteemp.setEmpno(eno);  
  109.   
  110. //out.println(deleteemp.toString());  
  111.   
  112. dao.delete(deleteemp);  
  113.   
  114. response.sendRedirect("index.jsp");  
  115.   
  116.   
  117. }  
  118.   
  119. %>  
  120.   
  121.                                         <form name="f1" action="deleteEmp.jsp" method="post">  
  122.   
  123.                                             <table width="471" border="1">  
  124.   
  125.                                                 <tr>  
  126.   
  127.                                                     <th> Employee No </th>  
  128.   
  129.                                                     <td><input name="eno" type="text" value="<%= num %>" onFocus="this.blur()">  
  130.   
  131.                                                     </td>  
  132.   
  133.                                                 </tr>  
  134.   
  135.                                                 <tr>  
  136.   
  137.                                                     <th>Employee Name </th>  
  138.   
  139.                                                     <td><input name="ename" type="text" value="<%= name %>"></td>  
  140.   
  141.                                                 </tr>  
  142.   
  143.                                                 <tr>  
  144.   
  145.                                                     <th>Salary </th>  
  146.   
  147.                                                     <td><input name="esal" type="text" value="<%= salary %>"></td>  
  148.   
  149.                                                 </tr>  
  150.   
  151.                                                 <tr>  
  152.   
  153.                                                     <th>Date of Birth</th>  
  154.   
  155.                                                     <td><input name="sd" type="text" id="datepicker" value="<%= dob %>" size=30></td>  
  156.   
  157.                                                 </tr>  
  158.   
  159.                                                 <tr>  
  160.   
  161.                                                     <th colspan="2"><input type="submit" onclick="if(confirm('Delete Record?'))  
  162.   
  163. alert('Record Deleted Successfully!'); else alert('A wise decision!')" name="s1" value="Delete">  
  164.   
  165.                                                     </th>  
  166.   
  167.                                                 </tr>  
  168.   
  169.                                             </table>  
  170.   
  171.                                         </form>  
  172.   
  173.                                 </body>  
  174.   
  175.                                 </html>  

9.UpdateEmp.jsp file code:

  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>  
  2.   
  3.     <%@page import="java.util.ArrayList"%>  
  4.   
  5.         <%@page import="com.model.pojo.Emp"%>  
  6.   
  7.             <%@page import ="java.util.Date"%>  
  8.   
  9.                 <%@page import = "java.text.SimpleDateFormat"%>  
  10.   
  11.                     <%@page import="com.dao.empDao"%>  
  12.   
  13.                         <%@page import="java.util.List"%>  
  14.   
  15.                             <jsp:useBean id="emp" class="com.model.pojo.Emp">  
  16.   
  17.                             </jsp:useBean>  
  18.   
  19.                             <%@ taglib prefix="display" uri="http://displaytag.sf.net"%>  
  20.   
  21.                                 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  22.   
  23.                                 <html>  
  24.   
  25.                                 <head>  
  26.   
  27.                                     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  28.   
  29.                                     <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">  
  30.   
  31.                                     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>  
  32.   
  33.                                     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>  
  34.   
  35.                                     <script type="text/javascript">  
  36.                                         $(document).ready(function() {  
  37.   
  38.                                             $("#datepicker").datepicker({  
  39.   
  40.                                                 showButtonPanel: true,  
  41.   
  42.                                                 showOn: "button",  
  43.   
  44.                                                 buttonText: "Select date",  
  45.   
  46.                                                 dateFormat: 'yy-mm-dd',  
  47.   
  48.                                                 changeMonth: true,  
  49.   
  50.                                                 changeYear: true,  
  51.   
  52.                                                 yearRange: "-90:"  
  53.   
  54.   
  55.                                             });  
  56.   
  57.                                         });  
  58.                                     </script>  
  59.   
  60.                                     <title>Update Employee</title>  
  61.   
  62.   
  63.                                 </head>  
  64.   
  65.                                 <body>  
  66.   
  67.                                     <%  
  68.   
  69. String sub=request.getParameter("s1");  
  70.   
  71. empDao dao = new empDao();  
  72.   
  73. String num="";  
  74.   
  75. int salary=0;  
  76.   
  77. String name="";  
  78.   
  79. Date dob=null;  
  80.   
  81. int empno = 0;  
  82.   
  83. if(sub == null){  
  84.   
  85. num=request.getParameter("empno");  
  86.   
  87. empno=Integer.parseInt(num);  
  88.   
  89. Emp editemp = dao.getEmpbyEmpNo(empno);  
  90.   
  91. //System.out.println(editemp.toString());//Debugging Purpose  
  92.   
  93. salary = editemp.getSal();  
  94.   
  95. name = editemp.getEname();  
  96.   
  97. //out.println(editemp.toString());//Debugging Purpose  
  98.   
  99. dob = editemp.getDob();  
  100.   
  101. }  
  102.   
  103. if(sub != null)  
  104.   
  105. {  
  106.   
  107. int neweno= Integer.parseInt(request.getParameter("eno"));  
  108.   
  109. String sd = request.getParameter("sd");  
  110.   
  111. String newename = request.getParameter("ename");  
  112.   
  113. int newSalary= Integer.parseInt(request.getParameter("esal"));  
  114.   
  115. //out.println(sd);  
  116.   
  117. if(sd != null)  
  118.   
  119. {  
  120.   
  121.   
  122. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  123.   
  124. Date dobdate = sdf.parse(sd);  
  125.   
  126. emp.setEmpno(neweno);  
  127.   
  128. emp.setEname(newename);  
  129.   
  130. emp.setSal(newSalary);  
  131.   
  132. emp.setDob(dobdate);  
  133.   
  134. dao.update(emp);  
  135.   
  136. %>  
  137.   
  138.                                         <script type="text/javascript" language="javascript">  
  139.                                             alert("Record Updated successful");  
  140.   
  141.                                             document.location = "index.jsp";  
  142.                                         </script>  
  143.   
  144.                                         <%  
  145.   
  146. // out.println(newename);//Debugging purpose  
  147.   
  148. // out.println(emp.toString());//Debugging purpose  
  149.   
  150.   
  151.   
  152. }  
  153.   
  154. }  
  155.   
  156. %>  
  157.   
  158.                                             <form name="f1" action="updateEmp.jsp" method="post">  
  159.   
  160.                                                 <table width="471" border="1">  
  161.   
  162.                                                     <tr>  
  163.   
  164.                                                         <th> Employee No </th>  
  165.   
  166.                                                         <td><input name="eno" type="text" value="<%= num %>" onFocus="this.blur()">  
  167.   
  168.                                                         </td>  
  169.   
  170.                                                     </tr>  
  171.   
  172.                                                     <tr>  
  173.   
  174.                                                         <th>Employee Name </th>  
  175.   
  176.                                                         <td><input name="ename" type="text" value="<%= name %>"></td>  
  177.   
  178.                                                     </tr>  
  179.   
  180.                                                     <tr>  
  181.   
  182.                                                         <th>Salary </th>  
  183.   
  184.                                                         <td><input name="esal" type="text" value="<%= salary %>"></td>  
  185.   
  186.                                                     </tr>  
  187.   
  188.                                                     <tr>  
  189.   
  190.                                                         <th>Date of Birth</th>  
  191.   
  192.                                                         <td><input name="sd" type="text" id="datepicker" value="<%= dob %>" size=30></td>  
  193.   
  194.                                                     </tr>  
  195.   
  196.                                                     <tr>  
  197.   
  198.                                                         <th colspan="2"><input type="submit" name="s1" value="Update">  
  199.   
  200.                                                         </th>  
  201.   
  202.                                                     </tr>  
  203.   
  204.                                                 </table>  
  205.   
  206.                                             </form>  
  207.   
  208.                                 </body>  
  209.   
  210.                                 </html>  

index.jsp

index

Update.jsp

Update

Modify the displayed record and click update.

Delete.jsp

Delete

Click Delete button to delete record after viewing it.


Similar Articles