Hibernater连接数据库

package com.minma;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class DAOHelper {

 private static SessionFactory sessionFactory;
 private static Log log = LogFactory.getLog(DAOHelper.class);

 public static SessionFactory getSessionFactory() {
  if(sessionFactory == null) {
   
   Configuration cfg = new Configuration();
   cfg.configure();
   
   log.info("Create Configuration instance...");
   sessionFactory = cfg.buildSessionFactory();
   log.info("Build SessionFactory instance...");
  }
  return sessionFactory;
 }
 
    public static void closeSession(Session hSession) {
        try{
            if(hSession != null){
                hSession.close();   
            }
        }catch(HibernateException he){
            //Ignore Excpeption
        }
    }
 
}

 

 

 

package com.minma.skin.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.minma.dao.DAOHelper;
/*author wenhao*/
public class CommonDAO {
 
 public boolean update(String operate,Object object){
  Session session = null;
  Transaction trx = null;
  boolean success=false;
  try { 
   SessionFactory sessionFactory = DAOHelper.getSessionFactory();
   session = sessionFactory.openSession();
   trx = session.beginTransaction(); 
    if (operate.equals("save")){
        session.save(object);
   }else if(operate.equals("delete")){
     session.delete(object); 
   }else if(operate.equals("update")){
     session.update(object);
   }
   success=true;
   trx.commit(); 
  }catch(Exception e){
   if(trx!=null)trx.rollback();
   success=false;
   e.printStackTrace();
  }finally{
   DAOHelper.closeSession(session);
  }
  return success;
    }
 
  public Object uniqueQuery(String hql){
 SessionFactory sessionFactory = DAOHelper.getSessionFactory();
 Session session = sessionFactory.openSession();
 Transaction tx=session.beginTransaction();
 Object object=null;
 try{
  Query query=session.createQuery(hql); 
  object=query.uniqueResult();
  tx.commit();
 }catch(Exception e){
   if(tx!=null){
  tx.rollback(); 
  } 
   e.printStackTrace();
 }finally{
   session.close(); 
 }
   return object;
  }
 
  public java.util.List query(String hql){
 SessionFactory sessionFactory = DAOHelper.getSessionFactory();
 Session session = sessionFactory.openSession();
 Transaction tx=session.beginTransaction();
 java.util.List list=null;
 try{
   Query query=session.createQuery(hql); 
   list=query.list();
   tx.commit();
 }catch(Exception e){
   if(tx!=null){
  tx.rollback(); 
   } 
   e.printStackTrace();
 }finally{
   session.close(); 
 }
 return list;
  }
 
  public java.util.List queryHQL(String hql,int size){
  SessionFactory sessionFactory = DAOHelper.getSessionFactory();
  Session session = sessionFactory.openSession();
  Transaction tx=session.beginTransaction();
  java.util.List list=null;
  try{
    Query query=session.createQuery(hql); 
    query.setMaxResults(size);
    list=query.list();
    tx.commit();
  }catch(Exception e){
    if(tx!=null){
   tx.rollback(); 
    } 
    e.printStackTrace();
  }finally{
    session.close(); 
  }
  return list;
   }
 
  public boolean delete(String hql){
 boolean success=false;
 SessionFactory sessionFactory = DAOHelper.getSessionFactory();
 Session session = sessionFactory.openSession();
 Transaction tx=session.beginTransaction();
 try{
   Query query=session.createQuery(hql); 
   query.executeUpdate();
   tx.commit();
   success=true;
 }catch(Exception e){
   if(tx!=null){
  tx.rollback(); 
   } 
   success=false;
   e.printStackTrace();
 }finally{
    session.close(); 
 }
 return success;
  }
  public java.util.List sqlQuery(String hql){
   java.util.List list=new java.util.ArrayList();
  SessionFactory sessionFactory = DAOHelper.getSessionFactory();
  Session session = sessionFactory.openSession();
  Transaction tx=session.beginTransaction();
  try{
    Query query=session.createSQLQuery(hql); 
    list=query.list();
    tx.commit();
  }catch(Exception e){
    if(tx!=null){
   tx.rollback(); 
    } 
    e.printStackTrace();
  }finally{
     session.close(); 
  }
  return list;
   }
}

 

你可能感兴趣的:(java,DAO,apache,Hibernate)