国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

使用JDBC4.0操作Oracle中BLOB類型的數據方法

2024-08-29 13:59:14
字體:
來源:轉載
供稿:網友

在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類型的數據的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到oracle教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新邵县| 广丰县| 衡南县| 会东县| 呼伦贝尔市| 大安市| 泽州县| 武夷山市| 巴楚县| 家居| 且末县| 常熟市| 巢湖市| 五莲县| 深泽县| 应用必备| 乌兰察布市| 吉木乃县| 桃园市| 玉山县| 沙洋县| 和静县| 三都| 佳木斯市| 岑巩县| 昭苏县| 桓台县| 宁明县| 义乌市| 安丘市| 东海县| 铁岭县| 宝山区| 石林| 洪江市| 木兰县| 即墨市| 宜丰县| 绵阳市| 吉木乃县| 玉屏|