Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
CommonDAO.java~3~
Package: shihua.rar [view]
Upload User: zghglow
Upload Date: 2022-08-09
Package Size: 27227k
Code Size: 10k
Category:
WEB(ASP,PHP,...)
Development Platform:
JavaScript
- package com.chinacannel.common;
- import java.util.List;
- import java.util.ArrayList;
- import net.sf.hibernate.*;
- import net.sf.hibernate.type.Type;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import java.io.Serializable;
- public class CommonDAO {
- private static Log log = LogFactory.getLog(CommonDAO.class);
- public CommonDAO() {
- }
- public int getCount(Class cls) throws DAOException {
- String hql = "select count(*) from " + cls.getName() + " s";
- return this.getCountBySql(hql);
- }
- public int getCountBySql(String hql) throws DAOException {
- Session session = null;
- int count = 0;
- try {
- session = HibernateUtil.currentSession();
- count = ((Integer) session.iterate(hql).next()).intValue();
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- }
- return count;
- }
- public boolean createObject(Object object) throws DAOException {
- boolean lpReturnValue = false;
- log.debug("创建对象的实例:" + object.getClass().toString());
- Session session = null;
- //Transaction trans = null;
- try {
- session = HibernateUtil.currentSession();
- // trans = session.beginTransaction();
- session.save(object);
- session.flush();
- // trans.commit();
- lpReturnValue = true;
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return lpReturnValue;
- }
- public boolean updateObject(Object object) throws DAOException {
- boolean lpReturnValue = false;
- log.debug("更新对象的实例:" + object.getClass().toString());
- Session session = null;
- //Transaction trans = null;
- try {
- session = HibernateUtil.currentSession();
- //trans = session.beginTransaction();
- session.update(object);
- session.flush();
- //trans.commit();
- lpReturnValue = true;
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return lpReturnValue;
- }
- public boolean removeObject(Object object) throws DAOException {
- boolean lpReturnValue = false;
- log.debug("删除对象的实例:" + object.getClass().toString());
- Session session = null;
- Transaction trans = null;
- try {
- session = HibernateUtil.currentSession();
- trans = session.beginTransaction();
- session.delete(object);
- trans.commit();
- //session.flush();
- lpReturnValue = true;
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- try {
- trans.rollback();
- } catch (HibernateException he2) {
- //
- }
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return lpReturnValue;
- }
- public Object loadObject(Serializable id, Class cls) throws
- DAOException {
- log.debug("获得对象的实例:" + cls.toString() + ",关键值为" + id.toString());
- Object po = null;
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- po = (Object) session.load(cls, id);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return po;
- }
- public Object loadOneObjectBySQL(String hql) throws DAOException {
- List list = this.loadObjectListBySQL(hql, 0, 1);
- if (list.size() > 0) {
- return list.get(0);
- } else {
- return null;
- }
- }
- public List loadObjectBySQL(String hql, Object[] params, Type[] types) throws
- DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- ls = session.find(hql, params, types);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- }
- return ls;
- }
- public List loadObjectListBySQL(String hql) throws DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- ls = session.find(hql);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // }finally{
- // try{
- // HibernateUtil.closeSession();
- // }catch(Exception ex){
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public List loadObjectListBySQL(String hql, Object[] params, Type[] types,
- int start, int top) throws DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- Query query = session.createQuery(hql);
- for (int i = 0; i < params.length; i++) {
- query.setParameter(i, params[i], types[i]);
- }
- query.setFirstResult(start);
- query.setMaxResults(top);
- ls = query.list();
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // }finally{
- // try{
- // HibernateUtil.closeSession();
- // }catch(Exception ex){
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public List loadObjectListBySQL(String hql, int start, int top) throws
- DAOException {
- List ls = new ArrayList(0);
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- Query query = session.createQuery(hql);
- query.setFirstResult(start);
- query.setMaxResults(top);
- ls = query.list();
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // }finally{
- // try{
- // HibernateUtil.closeSession();
- // }catch(Exception ex){
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public List loadAllObjects(Class cls) throws DAOException {
- List ls = null;
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- ls = session.find(getQuerySQL(cls));
- if (ls == null)
- ls = new ArrayList(0);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- // } finally {
- // try {
- // HibernateUtil.closeSession();
- // } catch (Exception ex) {
- // log.error(ex.getMessage(), ex);
- // }
- }
- return ls;
- }
- public int executeDelete(String hql) throws DAOException{
- Session session = null;
- try {
- session = HibernateUtil.currentSession();
- return session.delete(hql);
- } catch (UnresolvableObjectException uoe) {
- log.info(uoe.getMessage(), uoe);
- } catch (HibernateException he) {
- throw new DAOException("HibernateException:" + he.getMessage(), he);
- } catch (Exception ex) {
- throw new DAOException("Exception:" + ex.getMessage(), ex);
- }
- return -1;
- }
- protected String getQuerySQL(Class cls) {
- log.debug("from " + cls.getName() + " s");
- return "from " + cls.getName() + " s";
- }
- }