最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用JDBC4.0操作Oracle中BLOB類型的數(shù)據(jù)方法

 更新時(shí)間:2016年08月25日 09:32:31   作者:excalimax  
這篇文章主要介紹了使用JDBC4.0操作Oracle中BLOB類型數(shù)據(jù)的方法,我們需要使用ojdbc6.jar包,本文介紹的非常詳細(xì),需要的朋友可以參考下

在JDBC4.0推出后,它的從多的特性正在受到廣泛地關(guān)注。下面通過(guò)本文給大家介紹JDBC4.0操作Oracle中BLOB類型的數(shù)據(jù)的方法。

需要的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
); 

將文件寫(xiě)入數(shù)據(jù)庫(kù)

/**
* 將圖片文件存入數(shù)據(jù)庫(kù)
* @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.創(chuàng)建Blob
Blob image = DBHelper.getConnection().createBlob();
//2.將流放入blob
OutputStream out = image.setBinaryStream(1);
//3.讀取圖片,并寫(xiě)入輸出流
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});//自己簡(jiǎn)單封裝了jdbc操作
fis.close();
out.close();
return result;
}

將文件從數(shù)據(jù)庫(kù)中讀出

/**
* 將數(shù)據(jù)庫(kù)中的圖片文件讀出來(lái)
* @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";//指定輸出的目錄為項(xiàng)目下的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類型的數(shù)據(jù)的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

阿图什市| 亚东县| 周宁县| 通辽市| 武威市| 潼关县| 建德市| 平果县| 高台县| 健康| 射阳县| 龙游县| 满洲里市| 海淀区| 遂平县| 中卫市| 新竹市| 隆德县| 湟中县| 惠东县| 达日县| 曲沃县| 永仁县| 泰兴市| 赫章县| 瑞安市| 柘荣县| 德化县| 景德镇市| 准格尔旗| 卢氏县| 通城县| 鄂伦春自治旗| 岢岚县| 焉耆| 长子县| 扶风县| 五华县| 张家川| 潍坊市| 峨眉山市|