Wednesday, November 10, 2010

Hibernate Query Language-Aggregate function-avg()

Hibernate Query Language-Aggregate function-avg()


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

Problem:

            To get average 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 AvgExam

Include following coding into AvgExam

< %@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 Avg(aggregate function)Example < /title >
< /head >
< body >

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

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

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

Output:


Check the result

Open mysql

Given below query

mysql > select avg(tamil) from student;


No comments:

Post a Comment