Delete data from the database table Using Hibernate and JSP by
R4R Team
In the session of the Hibernate and struts2 integration tutorial we will learn how to delete data from the table using User Interfaces. Struts support the all operation which we want to perform. It can understand using the small example on the struts integration with Hibernate. For making the application we need to remember the all concept of hibernate and struts also then we can integrate together the all famework. For making on this application we need to go through the some basic step which is discuessed below:
For making these type of application we need to use any ide like as netbeans or it can be eclipes. Database will used here for both mysql. Here we need to fetch data from the database so we need to learn how to fetch data from the database. For this we need to go through the application example click here.
Step 1. First of all we take a simple Project in the Netbeans. First i would recommend to go in the Meny Bar -> the select to New Project (shift+ctrl+n) and follow the step and put the required name as we want to use of our application We will use here Struts2_Delete application name and then if we you have install the struts framework into the struts the you can select other wise click on finish. later you will need to add the jar library of the strus famework. Link to download the application framework jar click here. Download jar file of this Framework
Step 2. Now first of all we need to a database and a table also will be in that data which have data into that table. Because without data into the table we can't perform the action which we want to use. That table data will be fetched on the jsp page for performing the action.
Step 3. Create a database into the mysql database other wise we can use a single database which we have created as before.
create database R4R_Database;
use database R4R_Database;
Step 4. Create table inside the database.
create table DeleteData
(
id int(4) primary key,
fname varchar(50),
lname varchar(50),
email varchar(50),
addr varchar(50)
);
Above given table we will use in first for fetching the database stored into the table. Now we mannually insert the data into the datbase.
insert into DeleteData values(1,'Rahul','kumar','rahl@test.com','ghaziabad');
the above query we will use to insert data into the table.
Step 5. Here we will design that page which first fetch table data into the jsp page then we will perform our action using strust.xml mapping file.
DeletePage.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<%@ page import="java.util.*,R4R_Pojo.MyBean" %>
<% MyBean b; %>
<body bgcolor="lightyellow">
<center>
<h1>Struts2 Show The Fetch Record With Delete Action Using Hibernate Example</h1>
Step 6. Now we will attatch the hibernate configuration the connectivity of database. For this we need to go into the File menu wizard then select new file and search to hibernate and click on hibernate configuration file and click next and now here select the your database if now showing in the list then click on new connection and here write the name of your database and click on finsh.
hibernate cfg file code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Step 7. Now here we need a another file to completion the action of the hibernate cfg file that is hibernate revenge file. For this we need to go into the File menu wizard then select new file and search to hibernate and click on hibernate revenge file and click next and now here select the table of your database and click on finsh.
revenge file code is sow
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="hibernate_pro"/>
<table-filter match-name="MyBean"/>
</hibernate-reverse-engineering>
Step 8. Now create the two package. one for pojo class and another for the action class. For this we need to go into the File menu wizard then select new file and search to java and click on package and click next and put the name on the required field and click on finsh.
Step 9. Now at last we need to use to make pojo class we take the revenge file into our applicaion. For this we need to go into the File menu wizard then select new file and search to hibernate and click on hibernate pojo class file and click next and select the package in which we want to put this and click on finsh.
Step 10. Now we need to take an another java class for performing the action. For this we need to go into the File menu wizard then select new file and search to java and click on java class file put the required name and select package and click on finish.
ApplicationController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
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!