Hibernate

Deletion the Data through jsp page from the database using servlet in the Hibernate
Previous Home Next

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.

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.


 <%@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.


<%@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.


 <%@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:

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: