Tuesday, November 9, 2010

Hibernate Query Language-Aggregate function-min()

Hibernate Query Language-Aggregate function-min


Use:
Sometime we want to get lowest value from table,at that time we have to use max(aggregate function).

Problem:

            To get minimum mark of tamil from student table in navalady database.

Procedure:

Note:
           
  • Already we have created project named as HibernateProject.
  • Also we have created Folder for storing jsp and Hibernate files.










Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:           
            Create jsp file named as MaxExam

Include following coding into MaxExam
< %@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 >
< %@page import="org.hibernate.SessionFactory" % >
< %@page import="org.hibernate.Session" % >
< %@page import="org.hibernate.cfg.Configuration" % >
< %@page import="org.hibernate.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Min(aggregate function)Example < /title >
< /head >
< body >

< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();
String sql="select min(tamil) from Student student";

Query query=s.createQuery(sql);
List list=query.list();
out.println("Getting Minimum TAMIL Mark : "+list.get(0));

}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

Output:


Check the result

Open mysql

Given below query

mysql > select * from Student;



Hibernate Query Language-max()

Hibernate Query Language-Aggregate function-max


Use:
Sometime we want to get max value from table,at that time we have to use max(aggregate function).

Problem:

            To get maximum mark of mathematics from student table in navalady database.

Procedure:

Note:
           
  • Already we have created project named as HibernateProject.
  • Also we have created Folder for storing jsp and Hibernate files.










Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:           
            Create jsp file named as MaxExam

Include following coding into MaxExam

< %@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 >
< %@page import="org.hibernate.SessionFactory" % >
< %@page import="org.hibernate.Session" % >
< %@page import="org.hibernate.cfg.Configuration" % >
< %@page import="org.hibernate.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Max(aggregate function)Example < /title >
< /head >
< body >

< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();
String sql="select max(tamil) from Student student";

Query query=s.createQuery(sql);
List list=query.list();
out.println("Getting Maximum TAMIL Mark : "+list.get(0));

}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

output:



Check the result

Open mysql

Given below query

mysql > select * from Student;



Monday, November 8, 2010

Hibernate Query Language-count

Hibernate Query Language-count(Aggregate function)
Creating DataBase using Mysql


DataBase Name: Navalady

Table Name: Student

Open MYSQL command line Client


Step 1:

Create Database named as Navalady.

Syntax:

mysql > create Database Name;

mysql > create Navalady;


Step 2:

To use Created Database. We use the following statement.

Syntax:

mysql > use Database Name;

mysql > use Navalady;


Step 3:

Create Table named as Student.

Mysql > create table table_name(field_name datatypes,filed_name datatypes….);

Mysql > create table Student(stuname varchar(40),stuno int(20),tamil int(20),English int(20),maths int(20), science int(20),socialscience int(20),primary key(stuno));



Step 4:

Insert the records into Student table.

We have to insert five records.



















Hibernate Query Language-Aggregate function-count.

There are many aggregate function available in hibernate. Such as count, sum, max, min etc,

Use:
            Sometimes we want to get number of rows. At that time we use count() function to get total number of rows depending upon condition.

Example:

            Already we have created database as Navalady and table name is Student.

            Now we have to get how many students get hundred marks in mathematics.

Query is select count(*) from Student where maths=”100”;


Procedure:


Note:
· Already we have created project named as HibernateProject.· Also we have created Folder for storing jsp and Hibernate files.


Step 1:

            Create Hibernate Reverse Engineering wizard

Step 2:

             Create HibernateUtil.java

Step 3:
             
               Create POJO class and mapping files

Step 4: 

                 Create jsp file named as CountExam

Include following coding into CountExam

< %@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 >
< %@page import="org.hibernate.SessionFactory" % >
< %@page import="org.hibernate.Session" % >
< %@page import="org.hibernate.cfg.Configuration" % >
< %@page import="org.hibernate.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate count(aggregate function)Example < /title >
< /head >
< body >
< %

try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();
String sql="select count(*) from Student student where maths=100";
Query query=s.createQuery(sql);
Iterator it=query.iterate();

while(it.hasNext())
{
out.println("Total Studnet get 100 in maths "+it.next());
}
s.close();
}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >

OUTPUT:

The output is 0.

Because nobody get hundreds in mathematic subject.

Insert new records into the student table in navalady database;

Mysql > insert into Student values(‘Mahinda’,7,66,77,100,100,100);

Mysql > insert into Student values(‘Maahi’,8,66,77,100,100,100);


And Run the CountExam.jsp again.



Sunday, November 7, 2010

Hibernate Query Language-Select Clause

Hibernate:

AIM:
            We are going to select the record from table using select clause.

Syntax:
            Select employee.empname,employee.empno from Employee employee;

Procedure:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:           
            Create jsp file named as SelectClause


Include following coding into SelectClause



< %@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 >
< %@page import="org.hibernate.SessionFactory,org.hibernate.Session" % >
< %@page import="org.hibernate.cfg.Configuration" % >
< %@page import="org.hibernate.*" % >
< %@page import="Hiber.Employee" % >
< %@page import="java.util.*" % >
< html >
< head >
< title > Hibernate-using select clause < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

String sql="select employee.empname,employee.empno from Employee employee";
Query query=s.createQuery(sql);

Iterator it=query.iterate();
% >
< table border="5" >
< caption > < b > Example For Hibernate- Select Clause < /b > < /caption >
< tr > < th > EmployeeName < /th > < th > EmployeeID < /th > < /tr >
< %
while(it.hasNext())
{
Object[] obj=(Object[])it.next();
% >
< tr >
< td >
< %
out.println(obj[0]);
% >
< /td >
< td >
< %
out.println(obj[1]);
% >
< /td >
< /tr >
< %

}
% >
< /table >
< %
}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >


Output:





Structure Of Hibername Select Clause



Saturday, November 6, 2010

Hibernate Query Language- From Clause

Hibernate Query Language (HQL)


AIM:

            We use from clause in hibernate to select records from database


Syntax

From Employee employee;


Procedure:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:
            Create jsp file named as FromClause

           Include following coding into FromClause



< %@page import="org.hibernate.SessionFactory,org.hibernate.Session" % >
< %@page import="org.hibernate.cfg.Configuration,org.hibernate.*" % >
< %@page import="Hiber.Employee" % >
< %@page import="java.util.*" % >
< %@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 Page < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

String sql="from Employee employee";

Query query=s.createQuery(sql);
Iterator it=query.iterate();
% >
< table border="5" >
< caption > < b > Example For Hibernate- From Clause < /b > < /caption >
< tr > < th > EmployeeName < /th > < th > EmployeeID < /th > < /tr >
< %
while(it.hasNext())
{
Employee e=(Employee)it.next();
% >
< tr >
< td > < %= e.getEmpname() % > < /td >
< td > < %= e.getEmpno() % > < /td >
< /tr >

< %
}
% >
< /table >
< %
}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >


Step 5:

               Run the FromClause.

               Right click on FromClause.jsp and select Run

Step 6:


























Structure Of the Hibernate-From Clause


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.