還有其他幾種類型:
blob:二進制,如果exe,zip
clob:單字節碼,比如一般的文本文件.
nlob:多字節碼,如UTF格式的文件.
以下就是對CLOG字段的操作方法,在我們的項目中幫助文檔部分用到。
1、首先是寫入
- /* 以下表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字段內容
- /* 修改跟插入時基本一致,也是用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字段的文本內容
- /* 前面部分都一致 */
- 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);