jdbc blob oracle,使用JDBC4.0操作Oracle中BLOB类型的数据方法

在JDBC4.0推出后,它的从多的特性正在受到广泛地关注。下面通过本文给大家介绍JDBC4.0操作Oracle中BLOB类型的数据的方法。

需要的jar包

使用ojdbc6.jar

在/Meta-INF/MANIFEST.MF里可以看到Specification-Version: 4.0

建表

sql;">

create sequence seq_blobmodel_id start with 1 increment by 1 nocache;

create table blobmodel

(

blobid number(10) primary key not null,image blob

);

/**

@throws sqlException

@throws IOException

*/

public int writeBlob(String path) throws sqlException,IOException{

int result = 0;

String sql = "insert into blobmodel(blobid,image) values(seq_blobmodel_id.nextval,?)";

//1.创建Blob

Blob image = DBHelper.getConnection().createBlob();

//2.将流放入blob

OutputStream out = image.setBinaryStream(1);

//3.读取图片,并写入输出流

FileInputStream fis = new FileInputStream(path);

byte []buf = new byte[1024];

int len = 0;

while((len=fis.read(buf))!=-1){

out.write(buf,len);

}

result = DBHelper.executeUpdate2(sql,new Object[]{image});//自己简单封装了jdbc操作

fis.close();

out.close();

return result;

}

将文件从数据库中读出

sql;">

/**

@throws sqlException

@throws IOException

*/

public void readBlob() throws sqlException,IOException{

String sql = "select image from blobmodel where blobid=?";

DBHelper.getConnection();//

ResultSet rs = DBHelper.executeQuery(sql,new Object[]{1});

while(rs.next()){

Blob image = rs.getBlob(1);

InputStream is = image.getBinaryStream();

BufferedInputStream bis = new BufferedInputStream(is);

String path = "img/"+new Date().getTime()+".jpg";//指定输出的目录为项目下的img文件夹

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));

byte []buf = new byte[1024];

int len = 0;

while((len=bis.read(buf))!=-1){

bos.write(buf,len);

}

bos.close();

bis.close();

}

}

以上所述是小编给大家介绍的使用JDBC4.0操作Oracle中BLOB类型的数据的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程之家网站的支持!

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值