JAVA读取、写入、更新CLOB字段

来源:互联网 发布:linux 下安装vsftpd 编辑:程序博客网 时间:2024/06/06 18:02
Java代码  收藏代码
  1. /* 
  2. --建表语句如下: 
  3. create table t_clob( 
  4.         id varchar2(32) primary key, 
  5.         clobfield CLOB 
  6.         ); 
  7.  */  
  8.   
  9. /** 
  10.  * 读取CLOB字段的代码示例 
  11.  *  
  12.  * 作者:wallimn<br/> 
  13.  * 时间:2015-1-16<br/> 
  14.  * 联系:wallimn@sohu.com<br/> 
  15.  */  
  16. public void readClob() {  
  17.     //自定义的数据库连接管理类   
  18.     Connection conn = DbManager.getInstance().getConnection();  
  19.     try {  
  20.         PreparedStatement stat = conn  
  21.                 .prepareStatement("select clobfield from t_clob where id='1'");  
  22.         ResultSet rs = stat.executeQuery();  
  23.         if (rs.next()) {  
  24.             oracle.sql.CLOB clob = (oracle.sql.CLOB) rs  
  25.                     .getClob("clobfield");  
  26.             String value = clob.getSubString(1, (int) clob.length());  
  27.             System.out.println("CLOB字段的值:" + value);  
  28.         }  
  29.         conn.commit();  
  30.     } catch (SQLException e) {  
  31.         e.printStackTrace();  
  32.     }  
  33.   
  34.     DbManager.getInstance().closeConnection(conn);  
  35. }  
  36.   
  37. /** 
  38.  * 写入、更新CLOB字段的代码示例 
  39.  *  
  40.  * 作者:wallimn<br/> 
  41.  * 时间:2015-1-16<br/> 
  42.  * 联系:wallimn@sohu.com<br/> 
  43.  */  
  44. public void writeClob() {  
  45.     //自定义的数据库连接管理类   
  46.     Connection conn = DbManager.getInstance().getConnection();  
  47.     try {  
  48.         conn.setAutoCommit(false);  
  49.         // 1.这种方法写入CLOB字段可以。  
  50.         PreparedStatement stat = conn  
  51.                 .prepareStatement("insert into t_clob (id,clobfield) values(sys_guid(),?)");  
  52.         String clobContent = "This is a very very long string";  
  53.         StringReader reader = new StringReader(clobContent);  
  54.         stat.setCharacterStream(1, reader, clobContent.length());  
  55.         stat.executeUpdate();  
  56.   
  57.         // 2.使用类似的方法进行更新CLOB字段,则不能成功   
  58.         // stat.close();  
  59.         // stat =null;  
  60.         // stat =  
  61.         // conn.prepareStatement("update t_clob set clobfield=? where id=1");  
  62.         // stat.setCharacterStream(1, reader, clobContent.length());  
  63.         // stat.executeUpdate();  
  64.   
  65.         // 3.需要使用for update方法来进行更新,  
  66.         // 但是,特别需要注意,如果原来CLOB字段有值,需要使用empty_clob()将其清空。  
  67.         // 如果原来是null,也不能更新,必须是empty_clob()返回的结果。  
  68.         stat = conn  
  69.                 .prepareStatement("select clobfield from t_clob where id='1' for update");  
  70.         ResultSet rs = stat.executeQuery();  
  71.         if (rs.next()) {  
  72.             oracle.sql.CLOB clob = (oracle.sql.CLOB) rs  
  73.                     .getClob("clobfield");  
  74.             Writer outStream = clob.getCharacterOutputStream();  
  75.             char[] c = clobContent.toCharArray();  
  76.             outStream.write(c, 0, c.length);  
  77.             outStream.flush();  
  78.             outStream.close();  
  79.         }  
  80.         conn.commit();  
  81.     } catch (SQLException | IOException e) {  
  82.         // TODO Auto-generated catch block  
  83.         e.printStackTrace();  
  84.     }  
  85.   
  86.     DbManager.getInstance().closeConnection(conn);  
  87.   
  88. }  

原文转载 http://wallimn.iteye.com/blog/2176537
0 0
原创粉丝点击