• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

需要的jar包

使用ojdbc6.jar

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

建表

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, 0, len);
}
result = DBHelper.executeUpdate2(sql, new Object[]{image});//自己简单封装了jdbc操作
fis.close();
out.close();
return result;
}

将文件从数据库中读出

/**
* 将数据库中的图片文件读出来
* @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,0,len);
}
bos.close();
bis.close();
}
}

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MongoDB数据库的日志文件深入分析发布时间:2022-02-08
下一篇:
mysqldump你可能不知道的参数发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap