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

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

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

第三步,以及接下来的几步,我们来分别编写clob和blob的插入数据库和读出数据库的代码,新建com.test.InsertBlobData.java,顾名思义,它是用来插入blob类型数据的类。代码如下:

  1. class InsertBlobData{ 
  2.     private ResultSet rs = null; 
  3.     private InitDB idb = null; 
  4.  
  5.     InsertBlobData() 
  6.     { 
  7.         idb = new InitDB(); 
  8.     } 
  9.  
  10.     public void insertBlob(String sql1) throws SQLException 
  11.     { 
  12.         Connection con = idb.getCon(); 
  13.         try 
  14.         { 
  15.             con.setAutoCommit(false);// 不设置自动提交 
  16.             BLOB blob = null; // 插入空的Blob 
  17.             PreparedStatement pstmt = con 
  18.                     .prepareStatement("insert into cdl_test(sid,img) values(?,empty_blob())"); 
  19.             pstmt.setString(1, "100"); 
  20.             pstmt.executeUpdate(); 
  21.             pstmt.close(); 
  22.             rs = con.createStatement().executeQuery(sql1); 
  23.             while (rs.next()) 
  24.             { 
  25.                 System.out.println("rs length is:"); 
  26.                 Oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob("img"); 
  27.                 System.out.println("cloblength is:" + b.getLength()); 
  28.                 File f = new File("d:\\1.jpg"); //1.jpg一张QQ的截图 
  29.                 System.out.println("file path is:" + f.getAbsolutePath()); 
  30.                 BufferedInputStream in = new BufferedInputStream( 
  31.                         new FileInputStream(f)); 
  32.                 BufferedOutputStream out = new BufferedOutputStream(b 
  33.                         .getBinaryOutputStream()); 
  34.                 int c; 
  35.                 while ((c = in.read()) != -1) 
  36.                 { 
  37.                     out.write(c); 
  38.                 } 
  39.                 in.close(); 
  40.                 out.close(); 
  41.             } 
  42.             con.commit(); 
  43.         } 
  44.         catch (Exception e) 
  45.         { 
  46.             con.rollback();// 出错回滚 
  47.             e.printStackTrace(); 
  48.         } 
  49.     } 
  50. } 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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