使用JDBC4.0操作Oracle中BLOB類型的數(shù)據(jù)方法
在JDBC4.0推出后,它的從多的特性正在受到廣泛地關注。下面通過本文給大家介紹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
);
將文件寫入數(shù)據(jù)庫
/**
* 將圖片文件存入數(shù)據(jù)庫
* @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.讀取圖片,并寫入輸出流
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;
}
將文件從數(shù)據(jù)庫中讀出
/**
* 將數(shù)據(jù)庫中的圖片文件讀出來
* @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類型的數(shù)據(jù)的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Oracle 低權限數(shù)據(jù)庫賬戶得到 OS 訪問權限 提權利用
大概意思就是說,如果 ORACLE 服務是用 administrator 賬戶啟動的,你只要有一個具有 resource 和 connect 權限的數(shù)據(jù)庫賬戶,就能利用 metasploit 的 smbrelay 功能,本地搭建一個 SMB 欺騙服務器, 來得到系統(tǒng)的訪問權限。2009-06-06
CentOS系統(tǒng)上安裝配置Oracle數(shù)據(jù)庫的詳細教程
這篇文章主要介紹了CentOS系統(tǒng)上安裝配置Oracle數(shù)據(jù)庫的詳細教程,包括安裝過程中一些常見錯誤問題的解決,需要的朋友可以參考下2016-03-03
Oracle使用RMAN進行數(shù)據(jù)庫恢復的實現(xiàn)步驟
使用 RMAN(Recovery Manager)恢復 Oracle 數(shù)據(jù)庫是確保數(shù)據(jù)在災難情況下能夠得到恢復的關鍵步驟,以下是詳細的指導和代碼示例,展示如何使用 RMAN 進行數(shù)據(jù)庫恢復,需要的朋友可以參考下2024-09-09

