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

Tuesday, November 2, 2010

Hibernate Framework

Hibernate


Introduction:
Hibernate is an Open Source Object Relational mapping framework,
                       Using Hibernate you can add, edit, delete objects to and from a database.
                       

Why O/R mapping? - Why Object/Relational mapping?

1). Productivity
                        Hibernate help the developer to write complex SQL and no need JDBC API for result set and data handling. So developer takes more concentrate on business logic and increase productivity.

2). Maintain Ability
                        Hibernate helps to reduce the coding. So a system with less code is easier to maintain.

3). Free – Cost Effective

Hibernate is free and open source – Cost Effective

4). Learning curve is short 

 

5). Code generation tool

Hibernate tools provided by community helps for developer to generate or develop hibernate application very fast and easy.


Hibernate to run

Real time Example: to make coffee.

Requirements:

Real time example, we are going to prepare coffee. What are the requirements are needed to make coffee.

!) Any type of flame

2). Water

3). Coffee powder

4). Milk

5). Sugar

Procedure:
We follow some procedure to make coffee.
Step 1:  Boil the water
Step 2:  add sugar
Step 3:  add coffee powder
Step 4:  add milk
Step 5:  boil some more time
Step 6: filter
Coffee Ready to drink

 Hibernate to run

Requirements:

    Netbean 6.8

Procedure:
Step 1:
Create web application
Step 2:
Select Hibernate when we create web application.
Step 3:
Create reverse engineering
Step 4:
Create util
Step 5:
Create POJO classes
Step 6:
Create jsp

Next Post, we will learn how to create Hibernate application that is used to insert record into the database.

Friday, October 29, 2010

JSP Session Mgmt Using URL Rewriting

JSP- Session Management Using URL Rewriting

Introduction:

                    Someone disable cookies in their browser, when we can't use cookies to manage session in jsp. at that time, we use URLRewriting to manage session in jsp.

Wednesday, October 27, 2010

JSP Session Mgmt Using Servlet API


JSP- SERVLET API

Introduction:

We can also maintain session using Servlet API. All browser support servlet API for session management. HttpSession interface support session management. That mean, Servlet API provides HttpSession interface to implement session tracking. The HttpSession interface have many methods.

1). Get creating time: getCreationTime()-  it returns the creation time of the session.the time is returned as millisecongs since midnight of January 1, 1970.

2). Get session ID: getId()- it returns session ID for each session.

3). Get last accessed time:getLastAccessedTime(): it return the client;s last request using current session ID.

4). Get values of the session: getValueNames(): it returns the array of all values that are bound into the current session.

5). Get specific value:getValue()- it returns the specific object that are bound to the current session.


Problem:

They are four files: 1) session1.jsp 2). session2.jsp 3). session3.jsp   4). display.jsp
 
         Screen shot:

        session1.jsp
      
               Session2.jsp






















               Session3.jsp























Procedure:

             Step 1:

                            Create Project (Already You have not Project).

             Step 2:

                            Create Folder for storing jsp

             Step 3:

                            Create jsp file session1.jsp

Include following coding into the session1.jsp




< %@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 > JSP-Session API < /title >
< /head >
< body >
< form action="http://localhost:8084/SessionAPI/jsp/session2.jsp" method="post" >
FirstName:< input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

Create another jsp file session2.jsp

Include following coding into the session2.jsp

< %@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 > JSP-Session API < /title >
< /head >
< body >
< %
String fname=request.getParameter("fname");
% >
< %
HttpSession s=request.getSession();
s.setAttribute("f1",fname);
% >
< form action="http://localhost:8084/SessionAPI/jsp/session3.jsp" method="post" >

MiddleName > input type="text" name="mname"/ >
< input type="submit" value="OK"/ >

< /form >
< /body >
< /html >

Create another jsp file session3.jsp



< %@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 > JSP-Session API < /title >
< /head >
< body >
< %
String mname=request.getParameter("mname");
% >
< %
HttpSession s=request.getSession();
s.setAttribute("f2",mname);
% >
< form action="http://localhost:8084/SessionAPI/jsp/display.jsp" method="post" >
LastName < input type="text" name="lname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >



Create another jsp file display.jsp

Include following coding into the display.jsp


< %@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 > JSP-Session API < /title >
< /head >
< body >
< %
String lname=request.getParameter("lname");

% >
< %
HttpSession s=request.getSession(true);
s.setAttribute("f3",lname);
out.println(s.getAttribute("f1"));
out.println(s.getAttribute("f2"));
out.println(s.getAttribute("f3"));

% >

< /body >
< /html >


Step 4:

            Run the session1.jsp

            Give input for Firstname as Suresh


OK control goes to session2.jsp

Give input for Middlename as Kumar

Tuesday, October 26, 2010

JSP Session Mgmt-Using Cookie


JSP-Session Management using Cookies


Introduction:
                        Cookies are small information that is send to the server from browser for later use. When we manage session in jsp, cookies are used to manage session. There are four ways to manage the session. In this lesson, we will learn how to create cookies and how to use cookies in jsp. Cookie is encapsulated in javax.servlet.http.Cookie.

Create cookies:

                       
                        Cookie c=new Cookie(“CookieName”,”CookieValue”);
                        response.addCookie(c);

Retrieve cookies:

                       
                        Cookie[ ] display=request.getCookies();


Kill cookies:


Problem:

            There are three files. 1). cookie.jsp  2). cookie2.jsp 3). cookie3.jsp

cookie.jsp have one text box to get firstname and click OK the control goes to cookie2.jsp. cookie2.jsp have one text box to get lastname and click OK the control goes to cookie3.jsp. where we will display first name and last name .


Screen shot:

cookie.jsp

cookie2.jsp

cookie3.jsp
























Procedure:

Step 1:

            Create Project named as CookiesPro



Step 2:

            Create Folder named as jsp























Step 3:

3.1). Create jsp file named as cookie.jsp

< %@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 > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< br > < br >
< form action="http://localhost:8084/CookiesPro/jsp/cookie2.jsp" method="post" >
First Name: < input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

3.2). Create another jsp file named as cookie2.jsp

< %@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 > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b > < br >
< %

String fname=request.getParameter("fname");

%>
< %

Cookie c=new Cookie("firstname",fname);
response.addCookie(c);

% >
< form action="http://localhost:8084/CookiesPro/jsp/cookie3.jsp" method="post" >
Last Name < input type="text" name="lname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< html >

3.3). Create another jsp file named as cookie3.jsp



< %@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 > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< %

String lname=request.getParameter("lname");

% >

< %
Cookie c1=new Cookie("lastname", lname);
response.addCookie(c1);

% >


< form action="http://localhost:8084/CookiesPro/jsp/display.jsp" method="post" >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

3.4). Create another jsp file named as display.jsp

< %@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 > JSP-Display all Cookie < /title >
< /head >
< body >
< %
Cookie[] dis=request.getCookies();
for(int i=0;i
{
out.println(dis[i].getName()+":"+dis[i].getValue());
out.println("");
% >
< br >
< %
}
% >
< /body >
< /html >




Step 4:

            Run the cookie.jsp

            Suresh as Firstname

            Kumar as Lastname

Step 5:

            Get output


Structure of CookiePro


Friday, October 15, 2010

JSP Session Mgmt Using Hidden Form in jsp


JSP Hidden Form Field

Introduction:
                        From this post, we will understand the session management using Hidden Form Field. HTML form support input elements with type of Hidden.

Example:

&lt input type=”Hidden” name=”username” value=”sureshkumar”/ &gt

From example, the hidden filed’s value such as sureshkumar is passed with HTTP request. From the next page, we get this value by using request.getParameter() method.

Problem:

A.jsp file have one textbox to get Firstname.

B.jsp file have one textbox to get Lastname.

Result.jsp file display or print Firstname and Lastname.

Solution:

            Already we have seen this program without session. So we only B.jsp form value. We get A.jsp value as null.

Procedure:

Step1:
            Create Project name SessionHidden(Already we have seen how to create project for web development)

Step 2:
Create Folder named jsp to save jsp files.



Step 3:
           
Create jsp file named A.jsp

A.jsp have one textbox and button.

Screenshot

Create another jsp file named B.jsp

Screenshot


Step 4:

            Include following coding into A.jsp



< %@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 > A JSP Page < /title >
< /head >
< body >
< form method="post" action=http://localhost:8084/SessionHidden/Jsp/B.jsp >
Firstname < input type="text" name="fname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

Step 5:

Include following coding into B.jsp

< %@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 > B JSP Page < /title >
< /head >
< body >
< %
String fname=request.getParameter("fname");
% >
< form method="post" action=http://localhost:8084/SessionHidden/Jsp/Result.jsp >
Lastname < input type="text" name="lname"/ >
< input type="hidden" name="fname" value=" < %= fname % > " / >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

Step 6:

Include following coding into Result.jsp

< %@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 > Result JSP Page < /title >
< /head >
< body >
< %

String firstname=request.getParameter("fname");
String lastname=request.getParameter("lname");
out.println("FirstName: "+firstname);
out.println("< br > ");
out.println("LastName: "+lastname);

% >
< /body >
< /html >


Step 7:

            Run the A.jsp

            Screenshot of A.jsp

 
Control goes to B.jsp

Screenshot of B.jsp


Control goes to Result.jsp

Screenshot of Result.jsp


Successfully we get Firstname from A.jsp and we get Lastname from B.jsp.



Drawback of Hidden Form Field:
Every page must include form.