你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle存储大数据类型(Clob/Blob)

[日期:2012-10-17] 来源:Linux社区  作者:johnjobs [字体:大 中 小]

第四步,新建com.test.InsertClobData.java,用于插入clob数据类型的,代码如下:

  1. class InsertClobData 
  2. { 
  3.     private ResultSet rs = null; 
  4.     private InitDB idb = null; 
  5.  
  6.     InsertClobData() 
  7.     { 
  8.         idb = new InitDB(); 
  9.     } 
  10.  
  11.     public void insertClob(String sql1) throws SQLException 
  12.     { 
  13.         Connection con = idb.getCon(); 
  14.         try 
  15.         { 
  16.             con.setAutoCommit(false);// 不设置自动提交 
  17.             BLOB blob = null; // 插入空的Clob 
  18.             PreparedStatement pstmt = con 
  19.                     .prepareStatement("insert into cdl_test(sid,doc) values(?,empty_clob())"); 
  20.             pstmt.setString(1, "101"); 
  21.             pstmt.executeUpdate(); 
  22.             pstmt.close(); 
  23.             rs = con.createStatement().executeQuery(sql1); 
  24.             while (rs.next()) 
  25.             { 
  26.                 System.out.println("sdfasdfas"); 
  27.                 Oracle.sql.CLOB cb = (oracle.sql.CLOB) rs.getClob("doc"); 
  28.                 File f = new File("d:\\1.txt"); //1.txt一本小说《风云》马荣成 
  29.                 System.out.println("file path is:" + f.getAbsolutePath()); 
  30.                 BufferedWriter out = new BufferedWriter(cb 
  31.                         .getCharacterOutputStream()); 
  32.                 BufferedReader in = new BufferedReader(new FileReader(f)); 
  33.                 int c; 
  34.                 while ((c = in.read()) != -1) 
  35.                 { 
  36.                     out.write(c); 
  37.                 } 
  38.                 in.close(); 
  39.                 out.close(); 
  40.             } 
  41.             con.commit(); 
  42.         } 
  43.         catch (Exception e) 
  44.         { 
  45.             con.rollback();// 出错回滚 
  46.             e.printStackTrace(); 
  47.         } 
  48.     } 
  49. } 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款