Blob类型是一个
二进制格式的类型。
http://baike.baidu.com/link?url=Qsh_DL70A6q2CDvAmAx3l66XgwwjAX63wmiEYN1CPqtaxg1AesIv2gpZ_oY5QM4sp-BPo5p14-tTdViz5E8kT_
通过
hibernate我们可以将输入流转换转换成java.sql.Blob类型对象。
具体如下:
class="java">public class HibernateTest
{
private static SessionFactory sessionFactory;
private static ServiceRegistry sr;
static
{
try{
Configuration con=new Configuration().configure();
sr=new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry();
sessionFactory=con.buildSessionFactory(sr);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException
{
People people=new People();
InputStream is=new FileInputStream("d:/newfile.jpg");
int length=is.available();
byte[] buffer=new byte[length];
is.read(buffer);
Session session=sessionFactory.openSession();
Transaction tx=null;
people.setFile(session.getLobHelper().createBlob(buffer));
tx=session.beginTransaction();
session.save(people);
session.flush();
tx.commit();
}
}
People类中声明一个Blob对象,
private Blob file;
再通过session创建 org.hibernate.LobHelper对象,来完成这一存储过程就行。