/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ConToDB {
public static final ThreadLocal localsession = new ThreadLocal();
public static void ClossSession() throws HibernateException {
Session session = (Session) localsession.get();
localsession.set(null);
if (session != null)
session.close();
}
}
这个类是连接数据库的.我把它分开写了.
我还做了个测试类,就是从java文件直接保存这个图片文件(你也可以保存别的)到数据库:
/*
* Created on 2005-3-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package blob;
import net.sf.hibernate.HibernateException;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
*
* @hibernate.class
* table="Test.java"
* dynamic-update="true"
* dynamic-insert="true"
* optimistic-lock="version"
*/
public class Test {
public static void main(String args[]){
BlobTest bt = new BlobTest();
ProductTest p = new ProductTest();
try {
bt.insert(p, "18.jpg");
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
对了,刚才的ProductTest对应的XML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">