Fetch data form the database table using hibernate and jsp page by
R4R Team
In the session of the Hibernate and struts2 integraion tutorial we will learn how to Fetch 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 integraion with Hibernate. For making the application we need to remember the all concept of hibernate and strusts also then we can integrate together the all framework. For making on this application we need to go through the some basic step which is discussed 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.
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_Fetch 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 FetchData
(
id int(4) primary key,
fname varchar(50),
lname varchar(50),
email varchar(50),
addr varchar(50)
);
Above given table we will use in fiste for fetching the database stored into the table. Now we mannually insert the data into the datbase.
insert into FetchData 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.
FetchData.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 Without Action Using Hibernate Example</h1>
Step 6. Now we will attatech 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 finish.
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 finish.
revenge file code shown below:
<?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 finish.
Step 9. Now at last we need to use to make pojo class we take the revenge file into our application. 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 finish.
MyBean.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
public class MyBean implements java.io.Serializable {
private int sno;
private String sname;
private String scountry;
public MyBean() {
}
public MyBean(int sno) {
this.sno = sno;
}
public MyBean(int sno, String sname, String scountry) {
this.sno = sno;
this.sname = sname;
this.scountry = scountry;
}
public int getSno() {
return this.sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return this.sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getScountry() {
return this.scountry;
}
public void setScountry(String scountry) {
this.scountry = scountry;
}
}
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 finsh.
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!