Hibernate createsqlquery method: creates an sqlQuery object

createsqlQuery(String queryString)

Example

Session session = sessionFactory.openSession();  //创建Session对象
String sql = "select*from tb_user";  //指定sql语句
List list = null;
List rtnList = new ArrayList();  //创建保存查询结果的集合对象
try{
  Query query = session.createsqlQuery(sql);  //执行sql语句
  Iterator it = query.list().iterator();
  while(it.hasNext()){  //通过循环将查询到的用户信息保存到List集合中
    UserForm uf = new UserForm();  //定义保存用户信息的JavaBean对象
    Object[] o = (Object[])it.next();
    uf.setId((Integer)o[0]);
    uf.setUsername((String)o[1]);
    uf.setPwd((String)o[2]);
    rtnList.add(uf);  //向集合中添加对象
  }
}catch(Exception e){
  System.out.println("查询用户信息时的错误信息:"+e.getMessage());
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>