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



1 comment:

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    ReplyDelete