Deletion the Data through jsp page from the database using servlet in the Hibernate by R4R Team

For making these application we are using the NetBeans and appache tomcat server and mysql server. Here we will discuss how to update data into the database table without go in the database. Yes it can be possible when we work with the hibernate. For this we will take as we discussed in the last example how to fetch data form the database table data into the jsp page. It will help us to delete data because here we will use that jsp page to delete link we will provide into that jsp page. For doing the delete action using hibernate we will need to follow the some basic step which is given below:

Step 1. Here we will take the same appliation(HibernateApplicationUsingServlet) and database(hibernate_pro) for the update and delete data directely from the database.

In the above image we have saw the whre we need to perform the action. 

Step 2. Take a jsp file (FetchDataListWithAction) which is as given in last application activity. Now we need to edit to the code here will edit and provide the link of delete in a column. so we need to add a column.

This Below code will be generate with this file after change into the code:

<%@page import="org.hibernate.criterion.Restrictions" %>
<%@page import="org.hibernate.*" %>
<body>
    <table width="900" border="2"><tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Age</th><th>Last Update</th><th>Operation</th></tr>
        <%
        SessionFactory sf = r4r.HibernateUtil.getSessionFactory();
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();
        Criteria cr = s.createCriteria(r4r.InsertData.class);
        java.util.List<r4r.InsertData> ds = cr.list();
        for(r4r.InsertData dd : ds)
        {
            out.println("<tr><td>"+dd.getId()+"<td>"+dd.getFirstName()+"<td>"+dd.getLastName()+"<td>"+dd.getAge()+"<td>"+dd.getLastUpdate()+
                    "<td><a href=Delete.jsp?t="+dd.getId()+">Delete</a> "+" ");
        }
        %>   
    </table>
</body>

After build and run the project we found as we given below:

Step 3. Now we create a jsp page for action on Delete the data from the database. For this we take jsp file by going into the file menu wizard -> click on the new file -> put the file name (Delete) -> click on finish.

Here we provide the code for deletion for data from the table wihch we seeing in the image that is below:

<%@page import="org.hibernate.criterion.Restrictions" %>
<%@page import="org.hibernate.*" %>
<%
    int n = Integer.parseInt(request.getParameter("t"));
    SessionFactory sf = r4r.HibernateUtil.getSessionFactory();
    Session s = sf.openSession();
    Transaction tr= s.beginTransaction();
    r4r.InsertData r = (r4r.InsertData)s.get(r4r.InsertData.class, n);
    s.delete(r);
    tr.commit();
    response.sendRedirect("FetchListAfterDeletion.jsp");
    
%>

Step 4. Now we create a jsp page for delete the data from the database for this we take jsp file by going into the file menu wizard -> click on the new file -> put the file name (FetchDataListAfterDeletion) -> click on finish.

The Below code we will edit in the created file of jsp.

<%@page import="org.hibernate.criterion.Restrictions" %>
<%@page import="org.hibernate.*" %>
<body bgcolor="lightyellow">
    <table width="500" border="1"><tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Age</th><th>When Inserted</th></tr>
        <%
            SessionFactory sf = r4r.HibernateUtil.getSessionFactory();
            Session s = sf.openSession();
            Transaction tr = s.beginTransaction();
            Criteria cr = s.createCriteria(r4r.InsertData.class);
            java.util.List<r4r.InsertData> us = cr.list();
            for(r4r.InsertData dr : us)
            {
                
                out.println("<tr><td>"+dr.getId()+"</td><td>"+dr.getFirstName()+"</td><td>"+dr.getLastName()+"</td><td>"+dr.getAge()+"</td><td>"+dr.getLastUpdate()+"</td><tr>");
            }
            
        %> 
    </table>
    <%out.println("<h2>Record Successfully deleted");%>
</body>

Step 5. Now we build the application and run.

When we click on the action delete then we delete the record from the database as given in below:

With the help given below war file we can build the application.




Note: When we build the application war file then we need to put the file name.jsp in the url after building the application in appache tomcat.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!