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.
Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.
ReplyDelete