方法一:
在Oracle中,LOB(Large Object,大型物件)類型的欄位現在用得越來越多了。因為這種類型的欄位,容量大(最多能容納4GB的資料),且一個表中可以有多個這種類型的欄位,很靈活,適用於資料量非常大的業務領域(如圖象、檔案等)。而LONG、LONG RAW等類型的欄位,雖然儲存容量也不小(可達2GB),但由於一個表中只能有一個這樣類型的欄位的限制,現在已很少使用了。
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、往資料庫中插入一個新的CLOB對象
public static void clobInsert(String infile) throws Exception
{
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
}
2、修改CLOB對象(是在原CLOB對象基礎上進行覆蓋式的修改)
public static void clobModify(String infile) throws Exception
{
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
try {
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
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);
}
方法二:
Oracle中,Varchar2支援的最大位元組數為4KB,所以對於某些長字串的處理,我們需要用CLOB類型的欄位,CLOB欄位最大支援4GB。
還有其他幾種類型:
blob:二進位,如果exe,zip
clob:單位元組碼,比如一般的文字檔.
nlob:多位元組碼,如UTF格式的檔案.
以下就是對CLOG欄位的操作方法,在我們的項目中協助文檔部分用到。
1、首先是寫入
view plaincopy to clipboardprint?
/* 以下表PF_HELP_CONTENT中的HCONTENT欄位時CLOB類型的 */
// 通過序列器產生協助ID
Map map = Query.getMap("Select TO_CHAR(SEQ_HID.nextval) HID FROM DUAL ");
hid = String.valueOf(map.get("HID"));
//插入一條資料,注意CLOB欄位,需要先插入一個空的clob類型 empty_clob(),然後再單獨更新clob欄位
sql = "Insert INTO PF_HELP_CONTENT(HID,HCONTENT) VALUES (?,empty_clob()) ";
try
{
//執行插入
rtn = DbUtils.executeUpdate(sql,hid);
/* 插入成功後,修改HCONTENT欄位內容 */
//取得資料庫連接
Connection conn = DbUtils.getConnection();
//手動提交
conn.setAutoCommit(false);
//定義ResultSet 和 Clob 變數
ResultSet rs = null;
oracle.sql.CLOB clob = null;
//更新SQL
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
//hid是varchar2類型的,所以用setString
pstmt.setString(1,hid);
//執行update語句
rs= pstmt.executeQuery();
if(rs.next())
{
//取得剛才的HCONTENT的內容,也就是剛才添加的empty_clob()
clob = (oracle.sql.CLOB)rs.getClob(1);
}
//需要用clob.getCharacterOutputStream()流方式輸出
Writer write = clob.getCharacterOutputStream();
//寫入具體內容,helpform.getHContent() 存的是協助的內容
write.write(helpform.getHContent());
write.flush();
write.close();
rs.close();
//提交
conn.commit();
conn.close();
}
catch(Exception ex)
{
//.........
}
2、修改CLOB欄位內容
view plaincopy to clipboardprint?
/* 修改跟插入時基本一致,也是用for update來實現 */
//如果修改前的欄位內容長度大於當前修改的長度時,末尾的部分內容仍然會存在
//所以在修改內容前,需要將PF_HELP_CONTENT內容置空
sql = " Update PF_HELP_CONTENT SET HCONTENT=empty_clob() Where HID=? ";
try
{
rtn = DbUtils.executeUpdate(sql,hid);
//以下操作跟添加時一樣
Connection conn = DbUtils.getConnection();
conn.setAutoCommit(false);
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
pstmt.setString(1,hid);
rs= pstmt.executeQuery();
if(rs.next())
{
clob = (oracle.sql.CLOB)rs.getClob(1);
}
Writer write = clob.getCharacterOutputStream();
write.write(helpform.getHContent());
write.flush();
write.close();
rs.close();
conn.commit();
conn.close();
}
catch(Exception ex)
{
//...
}
3、取出CLOB欄位的常值內容
view plaincopy to clipboardprint?
/* 前面部分都一致 */
Connection conn = DbUtils.getConnection();
conn.setAutoCommit(false);
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
pstmt.setString(1,hid);
rs= pstmt.executeQuery();
if(rs.next())
{
//rs.getClob(1)中參數1指的是HCONTENT欄位索引,第一個欄位從1開始而不是從0。
//也可以用欄位名來取rs.getClob("HCONTENT")
clob = (oracle.sql.CLOB)rs.getClob(1);
}
if(clob==null || clob.length()==0)
{
hcontent = "";
}else
{
//取CLOB欄位內容為字串
hcontent=clob.getSubString((long)1,(int)clob.length());
}
rs.close();
conn.close();
request.setAttribute("HCONTENT",hcontent);
轉載自:http://blog.csdn.net/yoland/article/details/6455982