Hibernate NativeSQL: Get Records From Table
Using SQLQueries:
· Execution of native SQL queries is controlled via the SQLQuery interface,
· SQLQuery is calling through Session.createSQLQuery().
Example for SQLQuery:
String sql=”select * from Student student”;
List list=session.createSQLQuery(sql).list();
(OR)
String sql=”select student.stuname,student.stuno from Student student”;
List list=session.createSQLQuery(sql).list();
Procedure:
Create Hibernate Project as HibernateNativepro. |
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 NativeSelect.jsp
Include following coding into the NativeSelect.jsp
< %@page import="Hiber.Student"% >
< %@page import="java.util.Iterator"% >
< %@page import="java.util.List"% >
< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< html >
< head >
< title > Hibernate NativeSQL Example < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session ss=sessionFactory.openSession();
String sql="select * from Student stu";
List query=ss.createSQLQuery(sql).list();
Iterator it=query.iterator();
while(it.hasNext())
{
Object[] obj=(Object[])it.next();
out.println(obj[0]);
out.println(obj[1]);
out.println(obj[2]);
out.println(obj[3]);
out.println(obj[4]);
out.println(obj[5]);
out.println(obj[6]);
out.println(obj[7]);
}
}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >
Output:
No comments:
Post a Comment