dao.java

来自「采用struts+hibernet+javabean+jsp 些得shop购物网」· Java 代码 · 共 425 行

JAVA
425
字号
package com.hnzt.dao;

import java.util.List;

import org.hibernate.*;
import com.hnzt.hibernate.HibernateSessionFactory;
public class Dao {

	/**
	 * @param args
	 */
	private Session s;
	
	public Dao()
	{
		s = null;
	}
	
	public List checkLogin(String table,String userName,String passWord)
	{
		s = HibernateSessionFactory.getSession();
		List list = null;
		String hql = "from "+ table +" as table where user='"+ userName+"' and password='"+ passWord +"'"; 
		Query q = s.createQuery(hql);
		list = q.list();
		
		return list;
	}
	
	 public List Bbport(String hql) {
	       List resultlist = null;
	       try 
	       {
	           s = HibernateSessionFactory.getSession();

	           Query q = s.createQuery(hql);
	           
	           resultlist = q.list();

	           HibernateSessionFactory.getSession();
	       } catch (Exception e) {
	           e.printStackTrace();
	       }
	       finally {
	           try {
	               s.close();
	           } catch (Exception e) {
	               e.printStackTrace();
	           }
	       }
	       return resultlist;
	   }

    public int insertdb(Object obj) {
          try {
             s =   HibernateSessionFactory.getSession();
              Transaction t = s.beginTransaction();
              s.save(obj);
              s.flush();
              t.commit();

              return 1;
          } catch (Exception e) {
              e.printStackTrace();
          }
          finally {
              try {
            	  HibernateSessionFactory.getSession();
              } catch (HibernateException e) {
                  e.printStackTrace(); 
              }

          }
          return 0;
      }
    
    /**
     * **************************************************************************************************
     * 函数名:showdbwhere(String table,String condition,String value)
     * <p/>
     * 参数
     * : 操作表名,数据库中的查询条件字段名,条件值
     * 返回值
     * : list
     * 功能描述
     * : 返回请求的操作表的指定结果,无限制数量(只用于单一条件比对查询)
     * *******************************************************************************************************
     */
    public List showdbid(String table, String condition, String value) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
            String hql = "from " + table + " as table where " + condition + " = '" + value + "'";
            Query q = s.createQuery(hql);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
        	s.clear();
            e.printStackTrace();
        }
        
        return resultlist;
    }
    
    /**
     * **************************************************************************************************
     * 函数名:showdbwhere(String table)
     * <p/>
     * 参数
     * : 操作表名
     * 返回值
     * : list
     * 功能描述
     * : 返回请求的操作表的结果,无限制数量
     * *******************************************************************************************************
     */
    public List showAlldb(String table) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
            String hql = "from " + table + " as table ";
            Query q = s.createQuery(hql);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
        	s.clear();
            e.printStackTrace();
        }
        
        return resultlist;
    }
    
    /*******************************************************************************************************
     * 函数名:listdb(String table,int start,int pagenum)
     * <p/>
     * 参数: 操作表名,起始下标,终止下标
     * 返回值
     * : list
     * 功能描述
     * : 返回请求的操作表的指定结果,按ID排序
     * *******************************************************************************************************
     */
    public List listdb(String table, int start, int pagenum) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
             String hql = "from " + table + " as table where ishow='0' order by table.id";
             Query q = s.createQuery(hql);
            
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();

        } catch (Exception e) {
            e.printStackTrace();
        }       
        return resultlist;
    }
    
    /*******************************************************************************************************
     * 函数名:listdb(String table,int start,int pagenum)
     * <p/>
     * 参数: 操作表名,起始下标,终止下标
     * 返回值
     * : list
     * 功能描述
     * : 返回请求的操作表的指定结果,按ID排序
     * *******************************************************************************************************
     */
    public List listdb(String table, int start, int pagenum ,String name) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
             String hql = "from " + table + " as table where  name='" + name + "' order by table.id desc";
             Query q = s.createQuery(hql);
            
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();

        } catch (Exception e) {
            e.printStackTrace();
        }       
        return resultlist;
    }
    
    
    /*******************************************************************************************************
     * 函数名:listalldb(String table,int start,int pagenum)
     * <p/>
     * 参数: 操作表名,起始下标,终止下标
     * 返回值
     * : list
     * 功能描述
     * : 返回请求的操作表的指定结果,按ID排序
     * *******************************************************************************************************
     */
    public List listuserlyb(String table, int start, int pagenum,String number) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
             String hql =" select l.id, l.name,l.content,l.recontent,l.level,l.heading,l.ltime from Lyb as l,Customer as c  where l.name=c.user and c.operation = '"+ number +"'";
 //            String hql = "from " + table + " as table order by table.id";
             Query q = s.createQuery(hql);
            
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();

        } catch (Exception e) {
            e.printStackTrace();
        }       
        return resultlist;
    }
    
    public List addpro(String table, int start, int pagenum) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
             String hql = "from " + table + " as table where ishow='2' order by table.id";
             Query q = s.createQuery(hql);
            
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();

        } catch (Exception e) {
            e.printStackTrace();
        }       
        return resultlist;
    }
	
    public List shangdb(String table, int start, int pagenum) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
             String hql = "from " + table + " as table where ishow='1' order by table.id";
             Query q = s.createQuery(hql);
            
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            HibernateSessionFactory.closeSession();

        } catch (Exception e) {
            e.printStackTrace();
        }       
        return resultlist;
    }
    
    public boolean updatedb(Object obj) 
    {

        try {
            s = HibernateSessionFactory.getSession();
            Transaction t = s.beginTransaction();
            s.update(obj);
            s.flush();
            t.commit();
            HibernateSessionFactory.closeSession();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            s.close();
            return false;
        }
    }
    
    public int deletedb(Object obj) {
        int work = 0;
        try {
            s = HibernateSessionFactory.getSession();
            Transaction t = s.beginTransaction();
            s.delete(obj);
            t.commit();
            HibernateSessionFactory.closeSession();
            work = 1;
            return work;
        } catch (Exception e) {
            e.printStackTrace();
            return work;
        }
        finally {
            try {
            	HibernateSessionFactory.closeSession();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public Object getdb(String table, String id) {
        Object result = null;
        try {
            s = HibernateSessionFactory.getSession();

            Transaction t = s.beginTransaction();
            String hql = "from " + table + " as table where table.id="+ id;
            Query q = s.createQuery(hql);
    //        q.setParameter("id", id);
            result = q.list().get(0);
            t.commit();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
            	HibernateSessionFactory.closeSession();
            } catch (Exception e) {
            }
        }
        return result;
    }
    
    public List listalldb(String table) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
            Transaction t = s.beginTransaction();
            String hql = "from " + table + " as table order by table.id desc";
            Query q = s.createQuery(hql);
            resultlist = q.list();
            t.commit();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
            	HibernateSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultlist;
    }

    public List listdbdesc(String table, int start, int pagenum) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
            Transaction t = s.beginTransaction();
            String hql = "from " + table + " as table order by table.id desc";
            Query q = s.createQuery(hql);
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            t.commit();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
            	HibernateSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultlist;
    }
    
    ////////////////
    public List listalldb(String table, int start, int pagenum) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
            Transaction t = s.beginTransaction();
            String hql = "from " + table + " as table order by table.id desc";
            Query q = s.createQuery(hql);
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            t.commit();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
            	HibernateSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultlist;
    }
    
    public List listdblyb(String table, int start, int pagenum, String condition, String value) {
        List resultlist = null;
        try {
            s = HibernateSessionFactory.getSession();
            Transaction t = s.beginTransaction();
            String hql = "from " + table + " as table where " + condition + " = '" + value + "' order by table.id desc";
            Query q = s.createQuery(hql);
            q.setFirstResult(start);
            q.setMaxResults(pagenum);
            resultlist = q.list();
            t.commit();
            HibernateSessionFactory.closeSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
            	HibernateSessionFactory.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultlist;
    }
    
	public static void main(String[] args) {

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?