hibernate createquery_executequery方法出错

hibernate createquery_executequery方法出错/** *添加 */ publicvoidsave(Stustu){   try{    tran=this.GetSession().beginTransaction();    this.GetSession().save(stu);    tran.commit();   }catch(HibernateExceptione){

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

 

/** 

* 添加 

*/ 

public void save(Stu stu){ 

   try { 

    tran=this.GetSession().beginTransaction(); 

    this.GetSession().save(stu); 

    tran.commit(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

/** 

* 使用HQL全查询 

*/ 

public List getallbyHQL(){ 

   List arr=null; 

   try { 

    String hql=”from Stu”; 

    Query query=this.GetSession().createQuery(hql); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 



   /** 

* 根据主键查询 

*/ 

public Stu getbyID(int id){ 

   Stu stu=null; 

   try { 

    stu=(Stu) this.GetSession().get(Stu.class, id); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return stu; 

/** 

* 根据对象属性查询(使用Query) 

*/ 

public List getbyPropertyQuery(String name){ 

   List arr=null; 

   try { 

    //这里不能像SQL语一样select * from Stu where SName=:name,这是不对的。 

//    Query query=this.GetSession().createQuery(“from Stu where SName=:name”); 

//    query.setString(“name”, name); 

    //或者 

    Query query=this.GetSession().createQuery(“from Stu where SName=?”); 

    query.setString(0, name); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 

/** 

* 根据对象属性查询(使用Criteria) 

*/ 

public List getbyPropertyCriteria(String name){ 

   List arr=null; 

   try { 

    Criteria cri=this.GetSession().createCriteria(Stu.class); 

    Criterion c1=Expression.eq(“SName”, name); 

    cri.add(c1); 

    arr=cri.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 

/** 

* 查询部分属性 

*/ 

public List getProperty(){ 

   List arr=new ArrayList(); 

   try { 

    String hql=”select s.SName,s.SSex from Stu as s”; 

    Query query=this.GetSession().createQuery(hql); 

    List list=query.list(); 

    Iterator iter=list.iterator(); 

    while(iter.hasNext()){ 

     Object[] obj=(Object[]) iter.next(); 

     Stu s=new Stu(); 

     s.setSName(obj[0].toString()); 

     s.setSSex(obj[1].toString()); 

     arr.add(s); 

    } 

   } catch (HibernateException e) { 

    this.CloseSession(); 

   } 

   return arr; 



/** 

* 查询一个属性 

*/ 

public List getoneProperty(){ 

   List arr=new ArrayList(); 

   try { 

    String hql=”select s.SName from Stu as s”; 

    Query query=this.GetSession().createQuery(hql); 

    Iterator iter=query.iterate(); 

    while(iter.hasNext()){ 

     Object obj=(Object) iter.next(); 

     Stu s=new Stu(); 

     s.setSName(obj.toString()); 

     arr.add(s); 

    } 

   } catch (HibernateException e) { 

    this.CloseSession(); 

   } 

   return arr; 

/** 

*查询一个对象一个属性值 

*/ 

public Object getonlyProprotyValue(int s_id){ 

   Object obj=null; 

   try { 

    String hql=”select s.SName from Stu as s where s.SId=?”; 

    Query query=this.GetSession().createQuery(hql); 

    query.setInteger(0, s_id); 

    obj=query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return obj; 



/** 

* SQL查询 

*/ 

public List getallBYSQL(){ 

   List arr=null; 

   try { 

    String sql=”select {c.*} from stu as c”; 

    SQLQuery sqlquery=this.GetSession().createSQLQuery(sql); 

    sqlquery.addEntity(“c”,Stu.class); 

    arr=sqlquery.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 

/** 

* 根据对象查询 

*/ 

public List getallByObject(Stu s){ 

   List arr=null; 

   try { 

    String hql=”from Stu as s where s=:stuentity”; 

    //或者 

    //String hql=”from Stu as s where s.SId=:stuentity”; 

    Query query=this.GetSession().createQuery(hql); 

    query.setEntity(“stuentity”, s); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   }  

   return arr; 

/** 

* 模糊查询 

*/ 

public List getallQueryLike(String name){ 

   List arr=null; 

   try { 

    String hql=”from Stu as s where s.SName like :name”; 

    Query query=this.GetSession().createQuery(hql); 

    query.setString(“name”, “%”+name+”%”); 

    //不能 

    //query.setString(“name”, “‘%”+name+”%'”); 

    arr=query.list(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return arr; 



   /** 

* 统计函数 

*/ 

public int CountStu(){ 

   int count=0; 

   try { 

    String hql=”select count(*) from Stu”; 

    Query query=this.GetSession().createQuery(hql); 

    count=(Integer) query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return count; 

   /** 

* 条件统计 

*/ 

public int CountByWhere(String sex){ 

   int count=0; 

   try { 

    Query query=this.GetSession().createQuery(“select count(*) from Stu where SSex=:sex”); 

    query.setString(“sex”, sex); 

    count=(Integer)query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return count; 

/** 

* 统计平均值 

*/ 

public float VagAge(){ 

   float vag=0; 

   try { 

    Query query=this.GetSession().createQuery(“select avg(SAge) from Stu”); 

    vag=(Float)query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return vag; 

/** 

* 求和函数 

*/ 

public int sumage(){ 

   int sum=0; 

   try { 

    Query query=this.GetSession().createQuery(“select sum(SAge) from Stu”); 

    sum=(Integer)query.uniqueResult(); 

   } catch (HibernateException e) { 

    throw e; 

   }finally{ 

    this.CloseSession(); 

   } 

   return sum; 

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187574.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux ebpf_防火墙拦截dns解析

    linux ebpf_防火墙拦截dns解析自基于Netfilter的iptables取代ipchains之后,Linux防火墙技术貌似一直停留在iptables,虽然近年来nftables被宣称有取代iptables之势,但事实上并无起色。无论是晚期ipchains,还是iptables,或者nftables,其底层基础均是Netfilter,一个精心设计的五点HOOKs框架,在软件意义上,这个设计非常棒,但是涉及到单机性能问题,总是退…

    2022年9月21日
    1
  • python之open函数

    python之open函数1小序功能:打开某个文件,创建一个file对象,调用相关方法进行读写.格式:open(name[,mode[,buffering]])序号参数描述1name2mode3buffering

    2022年5月26日
    56
  • mysql批量插入大量数据「建议收藏」

    mysql批量插入大量数据「建议收藏」mysql批量插入大量数据时间:2020年11月25日今天遇到了一个批量插入大量数据任务,然后出于小白本能,直接for-each循环插入不就好了,于是手上开始噼里啪啦一顿操作,写好了从读取excel到插入数据库的工作,于是就美滋滋的开始了自己的测试,试了一把,一次通过perfect,然后后面就悲剧了,后面发现数据量稍微大一点,速度就会很慢很慢。于是掏出自己的制胜法典,后来我在知识和海洋中获取到了两种靠谱的方法。下面一点一点讲。测试的服务器信息1核2g5m的阿里云服务器(你懂得),mysql直接装

    2022年10月5日
    2
  • 手把手教你用Hexo+Github 搭建属于自己的博客「建议收藏」

    手把手教你用Hexo+Github 搭建属于自己的博客「建议收藏」欢迎关注我的微信公众号,扫一扫下方二维码或搜索微信号stormjun,即可关注。目前专注于Android开发,主要分享Android开发相关知识和一些相关的优秀文章,包括个人总结,职场经验等。闲聊在大三的时候,一直就想搭建属于自己的一个博客,但由于各种原因,最终都不了了之,恰好最近比较有空,于是就自己参照网上的教程,搭建了属于自己的博客。至于为什么要搭建自己的博客了?

    2022年7月27日
    5
  • pycharm配置tensorflow环境_tensorflow安装

    pycharm配置tensorflow环境_tensorflow安装1、Mac上安装tensorflow首先需要配置python环境,虽然Mac自带python2.7,但是做开发还是不够的,需要安装更高的版本,我这里安装的是python3.6.4,系统自带的版本最好不要去修改。2、python安装参考自博客:http://blog.csdn.net/fancylovejava/article/details/39140373因为我安装的是pyt

    2022年8月28日
    3
  • android layout 将EditText 和Button放在同一行

    android layout 将EditText 和Button放在同一行

    2021年7月9日
    72

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号