Blob和Clob的区别和用法
生活随笔
收集整理的這篇文章主要介紹了
Blob和Clob的区别和用法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
BLOB和CLOB都是大字段類型,
1、BLOB是按二進(jìn)制來存儲的,通常像圖片、文件、音樂等信息就用BLOB字段來存儲,先將文件轉(zhuǎn)為二進(jìn)制再存儲進(jìn)去。
2、CLOB是可以直接存儲文字的,像文章或者是較長的文字,就用CLOB存儲,這樣對以后的查詢更新存儲等操作都提供很大的方便。
Oracle中處理BLOB/CLOB字段的方式比較特別,所以需要特別注意下面兩點(diǎn):
正確的做法是:首先創(chuàng)建一個(gè)空 Blob/Clob 字段,再從這個(gè)空 Blob/Clob字段獲取游標(biāo),例如下面的代碼:
PreparedStatement ps=conn.prepareStatement("insert into PICTURE(image,resume) values(?,?)");//通過oralce.sql.BLOB/CLOB.empty_lob()構(gòu)造空Blob/Clob對象 ps.setBlob(1, oracle.sql.BLOB.empty_lob()); ps.setClob(2, oracle.sql.CLOB.empty_lob());ps.excuteUpdate(); ps.close();//再次對讀出Blob/Clob句柄 ps=conn.prepareStatement("select image,resume from PICTURE where id=? for update"); ps.setInt(1 , 100);ResultSet rs=ps.executeQuery(); rs.next(); oracle.sql.BLOB imgBlob=(oracle.sql.BLOB)rs.getBlob(1); oracle.sql.CLOB resClob=(oracle.sql.CLOB)rs.getClob(2);//將二進(jìn)制數(shù)據(jù)寫入Blob FileInputStream inStream=new FileInputStream("c://image.jpg"); OutputStream outStream=imgBlob.getBinaryOutputStream(); byte[] buf=new byte[10240]; int len; while(len=inStream.read(buf)>0){ outStream.write(buf, 0 ,len); } inStream.close(); outStream.cloese();//將字符串寫入Clob resClob.putString(1, "this is a clob");//再將Blob/Clob字段更新到數(shù)據(jù)庫 ps=conn.prepareStatement("update PICTURE set image=? and resume=? where id=?"); ps.setBlob(1, imgBlob); ps.setClob(2, resClob); ps.setInt(3, 100 );ps.executeUpdate(); ps.close();總結(jié)
以上是生活随笔為你收集整理的Blob和Clob的区别和用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle创建用户及授权等相关操作
- 下一篇: HTML 超链接