?? hiberdbimpl.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 + -