| 導購 | 订阅 | 在线投稿
分享
 
 
 

JDBC存取ORACLE大型數據對象LOB幾種情況的示範類

來源:互聯網  2008-05-31 22:08:06  評論

import Java.io.*;

import java.util.*;

import java.sql.*;

public class LobPros

{

/**

* Oracle驅動程序

*/

private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";

/**

* ORACLE連接用URL

*/

private static final String URL = "jdbc:oracle:thin:@test2000:1521:orac";

/**

* 用戶名

*/

private static final String USER = "user";

/**

* 密碼

*/

private static final String PASSWord = "pswd";

/**

* 數據庫連接

*/

private static Connection conn = null;

/**

* SQL語句對象

*/

private static Statement stmt = null;

/**

* @roseuid 3EDA089E02BC

*/

public LobPros()

{

}

/**

* 往數據庫中插入一個新的CLOB對象

*

* @param infile - 數據文件

* @throws java.lang.Exception

* @roseuid 3EDA04A902BC

*/

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 ID='111' 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);

}

/**

* 修改CLOB對象(是在原CLOB對象基礎上進行覆蓋式的修改)

*

* @param infile - 數據文件

* @throws java.lang.Exception

* @roseuid 3EDA04B60367

*/

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 ID='111' 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);

}

/**

* 替換CLOB對象(將原CLOB對象清除,換成一個全新的CLOB對象)

*

* @param infile - 數據文件

* @throws java.lang.Exception

* @roseuid 3EDA04BF01E1

*/

public static void clobReplace(String infile) throws Exception

{

/* 設定不自動提交 */

boolean defaultCommit = conn.getAutoCommit();

conn.setAutoCommit(false);

try {

/* 清空原CLOB對象 */

stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");

/* 查詢CLOB對象並鎖定 */

ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' 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);

}

/**

* CLOB對象讀取

*

* @param outfile - 輸出文件名

* @throws java.lang.Exception

* @roseuid 3EDA04D80116

*/

public static void clobRead(String outfile) throws Exception

{

/* 設定不自動提交 */

boolean defaultCommit = conn.getAutoCommit();

conn.setAutoCommit(false);

try {

/* 查詢CLOB對象 */

ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");

while (rs.next()) {

/* 獲取CLOB對象 */

oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");

/* 以字符形式輸出 */

BufferedReader in = new BufferedReader(clob.getCharacterStream());

BufferedWriter out = new BufferedWriter(new FileWriter(outfile));

int c;

while ((c=in.read())!=-1) {

out.write(c);

}

out.close();

in.close();

}

} catch (Exception ex) {

conn.rollback();

throw ex;

}

/* 恢複原提交狀態 */

conn.setAutoCommit(defaultCommit);

}

/**

* 向數據庫中插入一個新的BLOB對象

*

* @param infile - 數據文件

* @throws java.lang.Exception

* @roseuid 3EDA04E300F6

*/

public static void blobInsert(String infile) throws Exception

{

/* 設定不自動提交 */

boolean defaultCommit = conn.getAutoCommit();

conn.setAutoCommit(fal

  import Java.io.*;   import java.util.*;   import java.sql.*;      public class LobPros   {        /**     * Oracle驅動程序     */     private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";        /**     * ORACLE連接用URL     */     private static final String URL = "jdbc:oracle:thin:@test2000:1521:orac";        /**     * 用戶名     */     private static final String USER = "user";        /**     * 密碼     */     private static final String PASSWord = "pswd";        /**     * 數據庫連接     */     private static Connection conn = null;        /**     * SQL語句對象     */     private static Statement stmt = null;        /**     * @roseuid 3EDA089E02BC     */     public LobPros()     {        }        /**     * 往數據庫中插入一個新的CLOB對象     *     * @param infile - 數據文件     * @throws java.lang.Exception     * @roseuid 3EDA04A902BC     */     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 ID='111' 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);     }        /**     * 修改CLOB對象(是在原CLOB對象基礎上進行覆蓋式的修改)     *     * @param infile - 數據文件     * @throws java.lang.Exception     * @roseuid 3EDA04B60367     */     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 ID='111' 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);     }        /**     * 替換CLOB對象(將原CLOB對象清除,換成一個全新的CLOB對象)     *     * @param infile - 數據文件     * @throws java.lang.Exception     * @roseuid 3EDA04BF01E1     */     public static void clobReplace(String infile) throws Exception     {       /* 設定不自動提交 */       boolean defaultCommit = conn.getAutoCommit();       conn.setAutoCommit(false);          try {         /* 清空原CLOB對象 */         stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");         /* 查詢CLOB對象並鎖定 */         ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' 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);     }        /**     * CLOB對象讀取     *     * @param outfile - 輸出文件名     * @throws java.lang.Exception     * @roseuid 3EDA04D80116     */     public static void clobRead(String outfile) throws Exception     {       /* 設定不自動提交 */       boolean defaultCommit = conn.getAutoCommit();       conn.setAutoCommit(false);          try {         /* 查詢CLOB對象 */         ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");         while (rs.next()) {           /* 獲取CLOB對象 */           oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");           /* 以字符形式輸出 */           BufferedReader in = new BufferedReader(clob.getCharacterStream());           BufferedWriter out = new BufferedWriter(new FileWriter(outfile));           int c;           while ((c=in.read())!=-1) {             out.write(c);           }           out.close();           in.close();         }       } catch (Exception ex) {         conn.rollback();         throw ex;       }          /* 恢複原提交狀態 */       conn.setAutoCommit(defaultCommit);     }        /**     * 向數據庫中插入一個新的BLOB對象     *     * @param infile - 數據文件     * @throws java.lang.Exception     * @roseuid 3EDA04E300F6     */     public static void blobInsert(String infile) throws Exception     {       /* 設定不自動提交 */       boolean defaultCommit = conn.getAutoCommit();       conn.setAutoCommit(fal
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有