使用JDBC4.0操作Oracle中BLOB类型的数据方法
数据库  /  管理员 发布于 6年前   181
在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.创建BlobBlob image = DBHelper.getConnection().createBlob();//2.将流放入blobOutputStream 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类型的数据的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号