oracle中讀寫blob字段的問題解析建站知識
導讀:1建站知識這篇文章以程序實例說明通過JDBC操縱Oracle數據庫LOB類型字段的幾種情況網站建設網站推廣優化seo。
LOB類型分為BLOB和CLOB兩種:BLOB即二進制大型對像(Binary Large Object),適用于存貯非文本的字節流數據(如程序、圖像、影音等)。而CLOB,即字符型大型對像(Character Large Object),則與字符集相關,適于存貯文本型的數據(如歷史檔案、大部頭著作等)。 下面以程序實例說明通過JDBC操縱Oracle數據庫LOB類型字段的幾種情況。
先建立如下兩個測試用的數據庫表,Power Designer PD模型如下:
建表SQL語句為:CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)
一、 CLOB對象的存取
1、往數據庫中插入seo網站排名優化軟件一個新的CLOB對像
復制代碼 代碼網站seo優化如下:
public static void clobInsert(String infile) throws Exception{/* 設定不自動提交 */boolean defaultCommit = conn.getAutoCommit();conn.setAutoCommit(false);
try {/* 插入一個空的CLOB對像 */stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");/* 查詢此CLOB對象并鎖定 */ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE FOR UPDATE");while (rs.next()) {/* 取出此CLOB對像 */oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");/* 向CLOB對像中寫入數據 */BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());BufferedReader in = new BufferedReader(new FileReader(infile));int c;while ((c=in.read())!=-1) {out.write(c);}in.close();out.close();}/* 正式提交 */conn.commit();} catch (Exception ex) {/* 出錯回滾 */conn.rollback();throw ex;}
/* 恢復原提交狀態 */conn.setAutoCommit(defaultCommit);}
2、修改CLOB對像(是在原CLOB對像基礎上進行覆蓋式的修改)
復制代碼 代碼如下:
public static void clobModify(String infile) throws Exception{/* 設定不自動提交 */boolean defaultCommit = conn.getAutoCommit();conn.setAutoCommit(false);
try {/* 查詢CLOB對象并鎖定 */ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE FOR UPDATE");while (rs.next()) {/* 獲取此CLOB對像 */oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");/* 進行覆蓋式修改 */BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());BufferedReader in = new BufferedReader(new FileReader(infile));int c;while ((c=in.read())!=-1) {out.write(c);}in.close();out.close();}/* 正式提交 */conn.commit();} catch (Exception ex) {/* 出錯回滾 */conn.rollback();throw ex;}
/* 恢復原提交狀態 */conn.setAutoCommit(defaultCommit);}
3、替換CLOB對像(將原CLOB對像清除,換成一個全新的CLOB對像)
復制代碼 代碼如下:
public static void clobReplace(String infile) throws Exception{/* 設定不自動提交 */boolean defaultCommit = conn.getAutoCommit();conn.setAutoCommit(false);
聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-22 ,文章oracle中讀寫blob字段的問題解析建站知識主要講述字段,標簽,oracle中讀寫blob字段的問題解析建站知網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_5228.html