向oracle插入blob c语言,向Oracle中安插BLOB类型

向Oracle中插入BLOB类型

使用s2sh框架。实体类:

public class Photo implements java.io.Serializable {

// Fields

private Integer id;

private Album album;

private Timestamp createtime;

private String name;

private String contentType;

private Blob thumbnail;

private Blob content;

private Integer orderid;

private List facelookmarks = new ArrayList();

private Set facelookactivities = new HashSet(0);

private List facelookcomments = new ArrayList();

}

Photo.hbm.xml

action

FileInputStream fis = new FileInputStream(this.photoUpload);

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] b = new byte[1024];

int n;

while ((n=fis.read(b)) != -1) {

out.write(b,0,n);

}

fis.close();

out.close();

byte[] content = out.toByteArray();

this.photo.setAlbum(this.album);

this.photo.setContentType(this.photoUploadContentType);

this.photo.setCreatetime(new Timestamp(System.currentTimeMillis()));

this.photo.setOrderid(orderId);

this.photo.setContent(Hibernate.createBlob(content));

this.photo.setThumbnail(Hibernate.createBlob(thumbnail));

this.photoUpload为上传的文件。将得到的byte[]数组通过Hibernate.createBlob方法赋值给content和thumbnail属性。

PhotoDAO

getSession().save(photo);

getSession().flush();// 调用flush方法,强制Hibernate立即执行insert sql

getSession().refresh(photo, LockMode.UPGRADE);// 通过refresh方法,强制Hibernate执行select for update

这样在hibernate中就讲图片插入到数据库中了。

在JDBC中,需要先把BLOB字段插入空值,通过oracle.sgl.BLOB.empty_lob()方法构造空Blob对象。再次从库表读出,获得Blob句柄,然后将byte[]数组写入blob。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值