import net.sf.hibernate.Session;
import src.PersonSessionFactory;
import src.Person;
/**
* @author 杨强
*
*/
public class Queryhib {
public Iterator getPerson()
{
/*
* Use the ConnectionFactory to retrieve an open
* Hibernate Session.
*
*/
Session session = null;
try
{
session = PersonSessionFactory.currentSession();
/*
* Build HQL (Hibernate Query Language) query to retrieve a list
* of all the items currently stored by Hibernate.
*/
Query query =
session.createQuery(
"select person from Person person ");
return query.iterate();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/
}
public static void main(String[] args) throws Exception{
try{
Queryhib q=new Queryhib();
Iterator it=q.getPerson();
while(it.hasNext())
{
Person temp=(Person)it.next();
System.out.println(temp.getId());
System.out.println(temp.getName());
System.out.println(temp.getAddress());
}
}
catch (Exception ex)
{
System.err.println("Hibernate Exception" + ex.getMessage());
throw new RuntimeException(ex);
}
}
}
6.运行Insert.java对数据库进行插入操作;
7.运行Queryhib对数据库数据进行查询;
后注:是不是运行时会出现问题,那是因为没有配置log4j,可以将Hibernate下的log4j.properties放入该工程中一切OK了!没有贴图,请大家见谅!
共2页 1 2






