⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hiberdbimpl.java

📁 采用hibernate2.x框架,数据库采用sqlserver2000,封装了hibernage操作工具类
💻 JAVA
字号:
package com;

import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.Query;
import java.util.List;
import org.hibernate.Hibernate;
import java.sql.Blob;
import java.sql.Clob;
import java.io.InputStream;
import java.io.Reader;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class HiberDbImpl implements HiberDb 
{
    Query  query;
    List   resultList;
    Object resultSingle;
    Session session;
    Transaction tx;
    String format = "yyyy-MM-dd";
    
    public HiberDbImpl()
    {
	   	session = HibernateUtil.currentSession();
	    tx = session.beginTransaction();
    }

	public Object load(Object obj, Serializable id)
	{
		return session.load(obj.getClass(),id);
	}
	
	private void dbUpdate(Object o, int operType)
	{
		switch(operType)
		{
		case HiberDb.INSERT:
    	   	session.save(o);
			break;

		case HiberDb.UPDATE:
		    session.update(o);	
		    break;
		   	
		case HiberDb.DELETE:
		    session.delete(o);	
		    break;
		}
	}
	
	public void update(Object o, int operType)
	{
		dbUpdate(o, operType);
		tx.commit();
	}
	
	public void update(List l, int operType)
	{
		for(int i=0; i<l.size(); i++)
		{
			dbUpdate(l.get(i), operType);
		}
	
		tx.commit();		
	}

	public List	selectList(String sql)
	{
		
    	query = session.createQuery(sql);
    	resultList = query.list();
	 	return resultList;
	}
	
	public Object selectSingle(String sql)
	{
		resultSingle = null;
		
    	List l = selectList(sql);
    	if(l.size() > 0)
    		resultSingle = l.get(0);
    	
    	return resultSingle;
	}
	
	public List getResultList()	{return resultList;}
	public Object getResultSingle()	{return resultSingle;};
	
	public void open()
	{
		if(session == null)
		{
			session = HibernateUtil.currentSession();
	    	tx = session.beginTransaction();
	    }
	}

	public Clob toClob(String str){return Hibernate.createClob(str);}
	public Clob toClob(Reader reader,int length){return Hibernate.createClob(reader, length);}
	
	public Blob toBlob(InputStream in)
	{
		try
		{
			Blob bl = Hibernate.createBlob(in);
			return bl;
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		return null;
	}
	
	public Blob toBlob(byte[] in){return Hibernate.createBlob(in);}

	public void setFormat(String format){this.format = format;}
	
	public Date toDate(String date, String format)
	{
		SimpleDateFormat df = new SimpleDateFormat(format);
		try
		{ 
			return df.parse(date);
		}
		catch(Exception e)
		{
			System.out.println("格式错误");
		}
		
		return null;
	}
	
	public Date toDate(String date)	{return toDate(date, format);}

	public void close() {HibernateUtil.closeSession();}
}

⌨️ 快捷键说明

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