Thursday, November 4, 2010

Hibernate Setup Hibernate Example

Hibernate Framework

                                          -S.Sureshkumar,M.Sc,M.Phil,

MYSQL

Procedure:

            Step 1:

                        Download mysql( mysql is a open source DataBase).

            Step 2:

                        Open mysql.

            Step 3:
                        Create DataBase named as Hiber.

                        Using following sql query to create database.


mysql > create database Hiber;




We know whether the database is created or not.

If database is successfully created, Query OK, 1 row affected (0.01sec).



















 

Successfully We have Created database.


Step 4:

            Use Database Hiber

            Following query is used to use Hiber Database.


Mysql > use Hiber;



















Step 5:

Create table named Employee.

mysql > create table Employee(empname varchar(40),empno int,primary key(empno));




Step 6:

We can know structure of the table.

mysql > desc Employee;



 

 

Note: semi-colon is must at end of the query.



 Hibernate


Procedure:

Step 1:

            Open NetBean 6.8

Step 2:
            Create Project. Named HiberProject
           
            File—New Projectà New Project window will open.



















































Select Java Web under Categories

Select Web Application under Projects

Click Next

New Web Application window will open. Where you give project name as HibernateProject


























Click Next

Another New Web Application window will open.

Where we select server (I have selected Apache Tomcat 6.0.20)
























Click Next

New Web Application window will open.

Where we select Hibernate 3.2.5

Also select Database Connection as New Database Connection

























After select New Database Connection..

New Database Connection window will open.

Where we select like below.

Driver Name: MySQL(Connector/ J driver)

Host             : localhost

Port               :

Database       : Hiber

Username     :  root

Password      :  give your password

Display Name(Optional):    HiberDB( you give any name)

Select checkbox for Show JDBC URL

Screen shot:

























Click OK.

New Web Application window will open.



























Finish


Successfully Created Web Project named HibernateProject.

Step 3:

            Create Folder for storing jsp files.

Right click web pages and select New and select Folder – New Folder window will open. Where we give Folder name such as jsp.




























Finish


Successfully Folder named as jsp created.

Step 4:

Crete another Folder named as Hiber.

Right click on Source Package. Select – New – Folder.New Folder window will open. Where we give Folder name as Hiber.


Finish.

Successfully Folder named as Hiber created.

Step 5:

            Create Reverse Engineering

                       Right click on Hiber. Select—New—Other.




























New File window will open.

Select Hibernate under Categories

Select Hibernate Reverse Engineering Wizard under File Types.


























Next


New Hibernate Reverse Engineering Wizard window will open.


Where we give File Name. Such as hibernate.reveng

























Next

Another window will open like below

























Tables will display under Availabel Tables:.


Select employee and ADD >

The selected table will move into the Selected Tables.


























Finish


Successfully Hibernate Reverse Engineering created.

Step 6:

            Create HibernateUtil named as HibernateUtil

Right click on Hiber—New—Other—Hibernate—HibernateUtil.java


























Finish


Successfully HibernateUtil.java created.

Step 7:

            Create Hibernate Mapping files and POJO from Database.

Right click on Hiber—New—Other—Hibernate-- Hibernate Mapping files and POJO from Database.

























Next

Another window will open and where you don’t need to change. So just click

Finish


Successfully create POJO class named as Employee and mapping file named as Employee.hbm.xml

Step 8:

            Create jsp file named Home.jsp

Right click on jsp Folder and select New—other—web(under Categories)—jsp(under file types).



Next.

New jsp file window will open.

            Where we give jsp file name Home and 




Finish

Successfully Home.jsp file created.


Include following coding into the Home.jsp

< %@page import="org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration" % >
< %@page import="Hiber.Employee" % >
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > Hibernate Example < /title >
< /head >
< body >
< h1 > Hello Hibernate < /h1 >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();
Employee e=new Employee();
e.setEmpname("Anbuselvan");
e.setEmpno(1001);
s.beginTransaction();
s.save(e);
s.getTransaction().commit();
s.close();
sessionFactory.close();

out.println("Successfully records inserted into Database");
}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

Step 9:

Run the Home.jsp


Step 10:

If the record will be successfully inserted into table, the message will display will display like below.



Step 11:

Open mysql

mysql > use Hiber;

Step 12:

Give below query to select given records.

mysql > select * from Employee;



Structure of Hibernate application





That’s all.

God Bless You.

By Yours S.Sureshkumar, M.Sc, M.Phil

1 comment:

  1. Thanks for the post. It was very interesting and meaningful.

    ReplyDelete