Oracle中Blob和Clob類型的區別與操做

 

Oracle中Blob和Clob類型sql

一、Oracle中Blob和Clob類型的區別
  • BLOB和CLOB都是大字段類型,BLOB是按二進制來存儲的,而CLOB是能夠直接存儲文字的。其實兩個是能夠互換的的,或者能夠直接用LOB字段代替這兩個。可是爲了更好的管理ORACLE數據庫,一般像圖片、文件、音樂等信息就用BLOB字段來存儲,先將文件轉爲二進制再存儲進去。而像文章或者是較長的文字,就用CLOB存儲,這樣對之後的查詢更新存儲等操做都提供很大的方便  
二、Oracle中Blob和Clob類型的區別操做
  • LOB類型分爲BLOB和CLOB兩種:BLOB即二進制大型對像(Binary Large Object),適用於存貯非文本的字節流數據(如程序、圖像、影音等)。而CLOB,即字符型大型對像(Character Large Object),則與字符集相關,適於存貯文本型的數據(如歷史檔案、大部頭著做等)。

  三、下面以程序實例說明經過JDBC操縱Oracle數據庫LOB類型字段的幾種狀況。數據庫

     先創建以下兩個測試用的數據庫表,Power Designer PD模型以下:oracle

  • 建表SQL語句爲:
  • CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)
  • CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)

1、 CLOB對象的存取測試

一、往數據庫中插入一個新的CLOB對像 spa

代碼以下:code

  1. public static void clobInsert(String infile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 插入一個空的CLOB對像 */
  8. stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
  9. /* 查詢此CLOB對象並鎖定 */
  10. ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 取出此CLOB對像 */
  13. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  14. /* 向CLOB對像中寫入數據 */
  15. BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
  16. BufferedReader in = new BufferedReader(new FileReader(infile));
  17. int c;
  18. while ((c=in.read())!=-1) {
  19. out.write(c);
  20. }
  21. in.close();
  22. out.close();
  23. }
  24. /* 正式提交 */
  25. conn.commit();
  26. }
  27. catch (Exception ex) {
  28. /* 出錯回滾 */
  29. conn.rollback();對象

  30. throw ex;圖片

  31. }get

  32.  

    /* 恢復原提交狀態 */
  33. conn.setAutoCommit(defaultCommit);
  34. } 

二、修改CLOB對像(是在原CLOB對像基礎上進行覆蓋式的修改) it

代碼以下:
  1. public static void clobModify(String infile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查詢CLOB對象並鎖定 */
  8. ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
  9. while (rs.next()) {
  10. /* 獲取此CLOB對像 */
  11. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  12. /* 進行覆蓋式修改 */
  13. BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
  14. BufferedReader in = new BufferedReader(new FileReader(infile));
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. in.close();
  20. out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. }
  25. catch (Exception ex) {
  26. /* 出錯回滾 */
  27. conn.rollback();
  28. throw ex;
  29. }
  30. /* 恢復原提交狀態 */
  31. conn.setAutoCommit(defaultCommit);
  32. } 

三、替換CLOB對像(將原CLOB對像清除,換成一個全新的CLOB對像) 

代碼以下:
  1. public static void clobReplace(String infile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {

  7.  

    /* 清空原CLOB對像 */
  8. stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
  9. /* 查詢CLOB對象並鎖定 */
  10. ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 獲取此CLOB對像 */
  13. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  14. /* 更新數據 */
  15. BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
  16. BufferedReader in = new BufferedReader(new FileReader(infile));
  17. int c;
  18. while ((c=in.read())!=-1)
  19. {
  20. out.write(c);}in.close();out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. }
  25. catch (Exception ex)
  26. {
  27. /* 出錯回滾 */
  28. conn.rollback();
  29. throw ex;
  30. }
  31. /* 恢復原提交狀態 */
  32. conn.setAutoCommit(defaultCommit);
  33. } 

四、CLOB對像讀取 

代碼以下:
  1. public static void clobRead(String outfile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查詢CLOB對像 */
  8. ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
  9. while (rs.next()) {
  10. /* 獲取CLOB對像 */
  11. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  12. /* 以字符形式輸出 */
  13. BufferedReader in = new BufferedReader(clob.getCharacterStream());
  14. BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. out.close();
  20. in.close();
  21. }
  22. } catch (Exception ex) {
  23. conn.rollback();
  24. throw ex;
  25. }
  26. /*回覆元提交狀態

  27. conn.setAutoCommit(defaultCommit);

  28. }

     

2、 BLOB對象的存取

一、 向數據庫中插入一個新的BLOB對像 

代碼以下:
  1. public static void blobInsert(String infile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 插入一個空的BLOB對像 */
  8. stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
  9. /* 查詢此BLOB對象並鎖定 */
  10. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 取出此BLOB對像 */
  13. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  14. /* 向BLOB對像中寫入數據 */
  15. BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
  16. BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
  17. int c;
  18. while ((c=in.read())!=-1) {
  19. out.write(c);
  20. }
  21. in.close();
  22. out.close();
  23. }
  24. /* 正式提交 */
  25. conn.commit();
  26. }
  27. catch (Exception ex) {
  28. /* 出錯回滾 */
  29. conn.rollback();
  30. throw ex;
  31. }
  32. /* 恢復原提交狀態 */
  33. conn.setAutoCommit(defaultCommit);
  34. } 

二、修改BLOB對像(是在原BLOB對像基礎上進行覆蓋式的修改) 

 代碼以下:
  1. public static void blobModify(String infile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查詢BLOB對象並鎖定 */
  8. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
  9. while (rs.next()) {
  10. /* 取出此BLOB對像 */
  11. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  12. /* 向BLOB對像中寫入數據 */
  13. BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
  14. BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. in.close();
  20. out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. } catch (Exception ex) {
  25. /* 出錯回滾 */
  26. conn.rollback();
  27. throw ex;
  28. }
  29. /* 恢復原提交狀態 */

  30. conn.setAutoCommit(defaultCommit);

  31. }

     

三、替換BLOB對像(將原BLOB對像清除,換成一個全新的BLOB對像)

  代碼以下:

  1. public static void blobReplace(String infile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 清空原BLOB對像 */
  8. stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
  9. /* 查詢此BLOB對象並鎖定 */
  10. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 取出此BLOB對像 */
  13. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  14. /* 向BLOB對像中寫入數據 */
  15. BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
  16. BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
  17. int c;
  18. while ((c=in.read())!=-1) {
  19. out.write(c);
  20. }
  21. in.close();
  22. out.close();
  23. }
  24. /* 正式提交 */
  25. conn.commit();
  26. } catch (Exception ex) {
  27. /* 出錯回滾 */
  28. conn.rollback();
  29. throw ex;
  30. }
  31. /* 恢復原提交狀態 */

  32.  

    conn.setAutoCommit(defaultCommit);

  33. }

     

四、BLOB對像讀取 

代碼以下:
  1. public static void blobRead(String outfile) throws Exception
  2. {
  3. /* 設定不自動提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查詢BLOB對像 */
  8. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
  9. while (rs.next()) {
  10. /* 取出此BLOB對像 */
  11. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  12. /* 以二進制形式輸出 */
  13. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
  14. BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. in.close();
  20. out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. } catch (Exception ex) {
  25. /* 出錯回滾 */
  26. conn.rollback();
  27. throw ex;
  28. }
  29. /* 恢復原提交狀態 */

  30.  

    conn.setAutoCommit(defaultCommit);

  31. }

觀察上述程序對LOB類型字段的存取,咱們能夠看出,較之其它類型字段,有下面幾個顯著不一樣的特色:必須取消自動提交
相關文章
相關標籤/搜索