假设一个Student类的代码如下:
import java.sql.Blob;
import java.sql.Clob;
public class Student {
private String id; //主键id
private String name; //名字
private Integer age; //年龄
private Blob photo; //照片
private Clob describe; //对此学生的描述
//省略get和set方法
}
在进行对象保存时,可以使用Hibernate.createBlob()与Hibernate.createClob()从来源数据建立Blob与Clob实例,例如下面的代码将一幅照片和一段文本保存到数据库:
//新建并保存Student实例
FileInputStream fileInputStream = new FileInputStream("src\\sample.jpg");
Blob photo = Hibernate.createBlob(fileInputStream);
Clob describe = Hibernate.createClob("he is so good student");
stu.setName("tomclus");
stu.setAge(new Integer(30)); //设置年龄
stu.setPhoto(photo); //设置照片
stu.setDescribe(describe); //设置描述
session.save(stu);
……//提交事务,关闭session
import java.sql.Blob;
import java.sql.Clob;
public class Student {
private String id; //主键id
private String name; //名字
private Integer age; //年龄
private Blob photo; //照片
private Clob describe; //对此学生的描述
//省略get和set方法
}
在进行对象保存时,可以使用Hibernate.createBlob()与Hibernate.createClob()从来源数据建立Blob与Clob实例,例如下面的代码将一幅照片和一段文本保存到数据库:
//新建并保存Student实例
FileInputStream fileInputStream = new FileInputStream("src\\sample.jpg");
Blob photo = Hibernate.createBlob(fileInputStream);
Clob describe = Hibernate.createClob("he is so good student");
stu.setName("tomclus");
stu.setAge(new Integer(30)); //设置年龄
stu.setPhoto(photo); //设置照片
stu.setDescribe(describe); //设置描述
session.save(stu);
……//提交事务,关闭session
解决方案 »
- 如何用自定义标签实现图片验证码?
- struts2 request.setAttribute("address", "request中的值")之类的方法报指针异常错误。
- 看了很久没看出来哪里有问题,大家帮忙啊
- applicationContext配置文件问题
- 启动线程是用run()方法还是start()方法?一样吗?
- 求救!Strut2+hibernate3.3+Spring3整合出的错误.
- jsp/js询问关于IE7的tab关闭事件
- Unix与windows下换行符的问题??? 在程序中如何转换(小弟刚注册的号,没多少分)
- struts老问题,Action传置问题
- 谁知道在java中Excel按模板在另外一个里面克隆的方法??解决给400分和一套正版notes6.5
- 学习ajax.....遇到问题,在线等......
- 向大家请教一个关于数据库连结的问题
byte[] buffer = new byte[1];
buffer[0] = 1;
stu.setPhoto(Hibernate.createBlob(buffer));
session.save(stu);
session.flush();
//然后再更新, 代码比较怪,针对websphere+oracle
s.refresh(stu, LockMode.UPGRADE);
SerializableBlob blob = (org.hibernate.lob.SerializableBlob) stu.getPhoto();
Blob wb = blob.getWrappedBlob();
if (wb instanceof BLOB) {
BLOB image = (BLOB) wb;
OutputStream out = image.getBinaryOutputStream();
out.write(file1.getFileData()); //这里自己改成你的数据
out.close();
s.flush();
} else {
log.fatal("保存件失败, Blob类型不是oracle.sql.BLOB, 是 " +
wb.getClass().getName());
}
public byte[] readBLOB(String gjz) throws SQLException, IOException {
Session s = HibernateUtil.currentSession();
ZP0000 zp = (ZP0000) s.get(ZP0000.class, gjz);
if(zp==null) {
return null;
} InputStream in = zp.getZp0000().getBinaryStream(); Blob wb = ((SerializableBlob)zp.getZp0000()).getWrappedBlob();
int bufferSize = 0;
if (wb instanceof BLOB) {
bufferSize = ( (oracle.sql.BLOB) wb).getBufferSize();
}
else {
log.fatal("保存照片失败, Blob类型不是oracle.sql.BLOB, 是 " + wb.getClass().getName());
return null;
} ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] b = new byte[ (int) bufferSize];
int count = in.read(b, 0, (int) bufferSize);
int amount = 0;
while (count != -1) {
out.write(b, 0, count);
amount += count;
count = in.read(b, 0, (int) bufferSize);
}
out.close();
in.close();
return out.toByteArray();
}