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


相关推荐

  • IDEA的常见的设置和优化(功能)

    IDEA的常见的设置和优化(功能)显示工具条、设置鼠标悬浮提示、显示方法分隔符、忽略大小写提示、主题设置、自动导入包、单行显示多个Tabs、设置字体、配置类文档注释信息和方法注释模版、水平或者垂直显示代码、更换快捷键、注释去掉斜体、重装Idea导入配置信息

    2022年5月21日
    180
  • Java程序员是不是已经烂大街了?「建议收藏」

    Java程序员是不是已经烂大街了?「建议收藏」宽进严出,有人看一看面试题都可以混水摸鱼进去,进去容易,坚持下来的人很少,大部分都是代码搬运工,这些人其实不是严格的JAVA程序员,非常容易转到其他方面,比如做前端、测试、运维、产品、运营。能够坚持做5年以上JAVA开发的人不多。真正JAVA开发人员很缺,JAVA代码搬运工很多。这位网友说:中级程序员,高级程序员那就更缺了“烂大街”我可以认为是褒奖吧,行业在发展,从…

    2022年7月8日
    107
  • 数据库主键与外键

    数据库主键与外键主键概念 主关键字 primarykey 是表中的一个或多个字段 它的值用于唯一地标识表中的某一条记录 外键概念 外键 foreignkey 能保持数据的一致性 完整性主键用力啊标识数据的唯一性 而外键主要保证多张表的数据一致性 也就是需要保持多表同一数据的一致性 也许直接晦涩的解释其概念 对于起步接触数据库的人来说不太友好可以看如下实际用例 建一张 department 表 也可以把其理解为主表 这张主表用于记录部门 id 的唯一性 createtabled idintpr

    2025年6月22日
    4
  • 算法学习路线总结

    算法学习路线总结1.基础算法总结点击就可以查看相关博客文档讲解CreatedwithRaphaël2.2.0基础算法排序、查找算法选择排序冒泡排序插入排序

    2022年6月19日
    26
  • eclipse没有server项,解决办法「建议收藏」

    eclipse集成Tomcat:   打开eclipse-窗口-首选项-服务器-运行时环境找到Tomcat然后添加。eclipse添加插件:   开发WEB项目时要集成Tomcat可以并不是所有的eclipse都有服务器选项,如果不幸你的正好没有,不要怕。首先我们打开eclipse-help-installnewsoftware然后在workwith中输…

    2022年4月9日
    44
  • java classpath环境变量(linux配置java环境变量)

    刚学Java的时候,很多jdk配置教程都要求设置JAVA_HOME、Path、CLASSPATH3个变量。而Java官网有这么一句话:jdk1.5之后的版本在安装时不用设置CLASSPATH变量。今天我就以jdk1.5为例,总结下三者的区别。Path当我们安装完jdk之后,打开cmd(在非安装目录的路径下)输入javac、java,会提示找不到命令。我们需要将命令所在的路径添加到Path系…

    2022年4月15日
    100

发表回复

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

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