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;
}