使用JDBC4.0操作Oracle中BLOB类型的数据方法
编程学习 2021-07-05 16:08www.dzhlxh.cn编程入门
这篇文章主要介绍了使用JDBC4.0操作Oracle中BLOB类型数据的方法,我们需要使用ojdbc6.jar包,本文介绍的非常详细,需要的朋友可以参考下
在JDBC4.0推出后,它的从多的特性正在受到广泛地关注。狼蚁网站SEO优化通过本文给大家介绍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类型的数据的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- ThinkPad 笔记本如何调节LCD屏幕亮度
- Dreamweaver制作网页打开特效教程
- Win10 Mobile 10586升级后无限重启怎么办 硬重启帮您
- Win8系统提示音频设备有问题有一个或多个音频服
- Xbox One版Win10首个预览版9月份发布
- 如何在textarea文本输入区内实现换行
- Win10 Build 9901系统更新 预览版新版本下载
- McAfee Framework存在远程格式串处理漏洞
- Win10家庭版今日(7月30)正式在中国官方商城开卖
- Win10 Mobile预览版更新完10536.1000后才收到10536.100
- Win10 RS2更新了什么-Win10 RS2最终版本号1704首曝
- Windows7如何查看回收站对应的文件夹有哪些方法
- Win10让Charms栏回归桌面的方法教程
- 取消Windows XP系统开机启动画面的小技巧
- win8系统怎么下载安装USB百兆网卡?
- XP系统下磁盘空间变少了怎么办?XP系统磁盘空间