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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android bindservice方法,Android bindservice方法返回false

    android bindservice方法,Android bindservice方法返回false我想从另一个类(BaseExpandableListAdapter)的活动中调用一个方法。活动中的方法启动服务并调用bindService(,,)方法。但是,bindService方法总是返回false。我查了其他类似的帖子,但没有一个解决了我的问题。任何评论非常感谢。Androidbindservice方法返回false这里是BaseExpendableListAdapter类中,我调用该方法…

    2022年5月27日
    53
  • TI C66x DSP 系统events及其应用 – 5.1(QM accumulator的配置)

    TI C66x DSP 系统events及其应用 – 5.1(QM accumulator的配置)

    2021年12月1日
    48
  • inputstreamreader和inputstream_InputStream

    inputstreamreader和inputstream_InputStreampackagecsdn.java3;importorg.junit.Test;importjava.io.*;/***处理流之二:转换流的使用*1.转换流:属于字符流*InputStreamReader:将一个字节的输入流转换为字符的输入流*OutputStreamWriter:将一个字符的输出流转换为字节的输出流**2.作用:提供字节…

    2022年9月25日
    0
  • Dijkstra算法和Floyed算法「建议收藏」

    Dijkstra算法和Floyed算法「建议收藏」Dijkstra算法和Floyed算法最短路径:在非网图中,最短路径是指两顶点之间经历的边数最少的路径。在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径。最短路径问题:单源点到其他顶点的最短路径:Dijkstra方法,O(n2)按路径长度递增任意一对顶点之间的最短路径:Floyed方法,O(n3)Dijkstra算法:按路径长度递增1.设置一个集合S存放已经找到最短…

    2022年6月16日
    25
  • directx修复工具强力修复(Linux可用的工具)

    DirectX修复工具最新版:DirectXRepairV3.7增强版  NEW!版本号:V3.7.0.26539大小:107MB/7z格式压缩,189MB/zip格式压缩,322MB/解压后其他版本:标准版   在线修复版MD5校验码:DirectXRepair.exe/0615325098da4e624ef854af60b56ba2       DirectX_…

    2022年4月10日
    364
  • python漫画阅读器 漫画网站只能左右翻页,没法上下滚动连续下拉式观看且广告多体验差?因涉及“版权不明”, 审核未通过

    python漫画阅读器 漫画网站只能左右翻页,没法上下滚动连续下拉式观看且广告多体验差?因涉及“版权不明”, 审核未通过没钱看正版漫画,盗版漫画网站只能左右翻页,没法上下滚动观看且广告多体验差?于是我写了个python爬虫。手机上无论是收费还是免费盗版的漫画都有各种各样的app可供选择,正版的像是腾讯动漫,哔哩哔哩漫画,菠萝包等等,免费的比如动漫之家,免费搜书大全阅读器等等。(说是搜书其实也能看漫画,本质就是一个爬虫);而且阅读的体验也都很不错,且大部分时候也都是在手机上阅读。但当我心血来潮在笔记本上看盗版漫画(穷学生一个)的时候发现很多的盗版漫画网站只有左右翻页观看,翻页很累,而且图片老大一张,网站又没有漫画图片大小调整

    2022年6月16日
    36

发表回复

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

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