Hibernate的HQL查询及原生sql查询语句写法

HQL语句查询,其中省略session的生成过程(从0开始set值)


public String getHistorySqZc(String zxjxjhh,String jsh,String kch,String kxh,
Long skjc, Long skxq, String oper) throws Exception {
    String historyZc = null;
    try {
        Session session = HibernateUtil.currentSession(zhjw.sysconf.     //此处省略生成的session过程(为共通方法生成)
            SysConstants.DB_EESDB);
        String queryString ="select a.skzc from zhjw.data.jxyxgldllg.JxyxTkjlb a,zhjw.data.jxyxgldllg.JxyxTksqb b " +
        "where a.id.zxjxjhh=? and a.id.kch=? and a.id.kxh=? " +
        "and a.id.jsh=? and a.skjc=? and a.skxq=? and a.id.zxjxjhh=b.id.zxjxjhh "+
        "and a.id.kch=b.id.kch and a.id.kxh=b.id.kxh and a.id.jsh=b.id.jsh " +
        "and a.id.sqbh = b.id.sqbh and b.sqztdm='01'";
        Query query = session.createQuery(queryString);
        query.setString(0, zxjxjhh);
        query.setString(1, kch);
        query.setString(2, kxh);
        query.setString(3, jsh);
        query.setLong(4, skjc);
        query.setLong(5, skxq);
        ArrayList tmpList = (ArrayList)query.list();
        if(tmpList!=null&&tmpList.size()!=0){
            for(int i=0;i<tmpList.size();i++){
            Object skzc = (Object)tmpList.get(i);
                if(historyZc==null){
                    historyZc = skzc.toString();
                }else{
                    historyZc = Tool.andCompute(historyZc,skzc.toString());
                }
            }
        }
    }
    catch (Exception e) {
        historyZc = null;
        throw e;
    }
    finally {
        HibernateUtil.closeSession();
    }
    return historyZc;
}


原生sql的查询(preparestatement,set从1开始)

--也可以rs.getString(1)...

public List<JasJyInfo> queryCodeJasbViewB(String cxtj)
    throws Exception
  {
    List list = new ArrayList();
    Connection con = null;
    PreparedStatement pre = null;
    try {
      Session session = 
        HibernateUtil.currentSession("/hibernateC.cfg.xml");
      con = session.connection();
      String sql = "select a.xqh,a.jxlh,a.jash,a.xqm,a.jxlm,a.jasm,d.sqbh from code_jasb_view a,(select b.sqbh,b.xqh,b.jxlh,b.jash from Jas_Jysqb_Spjg b where exists (select sqbh from Jas_Jysqb c where 1=1 " + 
        cxtj + 
        " and b.sqbh = c.sqbh)) d " + 
        "where a.xqh = d.xqh and a.jxlh=d.jxlh and a.jash=d.jash" + 
        " order by a.xqh,a.jxlh,a.jash";
      pre = con.prepareStatement(sql);

//若有占位符? 此处即是书写占位符的位置   例如pre .setString(1, "传输的数据");
     ResultSet rs=pre.executeQuery();
      while ((rs != null) && (rs.next())) {
        JasJyInfo obj = new JasJyInfo();
        String xqh = rs.getString("xqh");
        String jxlh = rs.getString("jxlh");
        String jash = rs.getString("jash");
        String xqm = rs.getString("xqm");
        String jxlm = rs.getString("jxlm");
        String jasm = rs.getString("jasm");
        String sqbh = rs.getString("sqbh");
        obj.setXqh(xqh);
        obj.setXqm(xqm);
        obj.setJxlh(jxlh);
        obj.setJxlm(jxlm);
        obj.setJash(jash);
        obj.setJasm(jasm);
        obj.setSqbh(sqbh);
        list.add(obj);
      }
      pre.close();
      con.close();
    } catch (Exception e) {
      e.printStackTrace();
      if (pre != null) pre.close();
      if (con != null) con.close(); 
    }
    finally { HibernateUtil.closeSession(); }


    return list;
  }



原生sql的查询(callablestatement,set从1开始)

public boolean sfqfBySfb(String zxjxjhh, String xh) throws Exception{
Connection con = null;
CallableStatement cs = null;
try {
Session session = HibernateUtil.currentSession(zhjw.sysconf.SysConstants.DB_EESDB);
con = session.connection();
cs = (CallableStatement)con.prepareCall("{call p_get_sfqk(?,?,?)}");
cs.setString(1, zxjxjhh);
cs.setString(2, xh);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
String rs = cs.getString(3);
System.out.println("rs:"+rs);
if("Y".equals(rs)){
return true;
}else{
return false;
}
} catch (HibernateException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
            if(cs!=null){
cs.close();
            }
            if(con!=null){
            con.close();
            }
HibernateUtil.closeSession();
}
return true;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值