Vincent.Chan‘s Blog

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          公告

          本博客僅為收集留待閱讀。在此對原作者表示感謝。
          ---------------------------

          留言簿(11)

          隨筆分類

          文章分類

          隨筆檔案

          文章檔案

          相冊

          閱讀排行榜

          評論排行榜

          常用鏈接

          統(tǒng)計

          積分與排名

          網(wǎng)站

          最新評論

          JDBC實現(xiàn)oracle blob 出庫入庫

          1,首先建一個足夠簡單的表imageTable
                id         varchar(20)
                image   blob
          2,blob入庫
                拷貝Sunset.jpg圖片在C盤根目錄下     
           1      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 
           2      Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:beyondduke","duke","duke"); 
           3      conn.setAutoCommit(false); 
           4      BLOB blob = null
           5      PreparedStatement pstmt = conn.prepareStatement("insert into imageTable(id,image) values(?,empty_blob())"); 
           6      pstmt.setString(1,"10001"); 
           7      pstmt.executeUpdate(); 
           8      pstmt.close(); 
           9      pstmt = conn.prepareStatement("select content from imageTable where id= ? for update"); 
          10      pstmt.setString(1,"10001"); 
          11      ResultSet rs = pstmt.executeQuery(); 
          12     if (rs.next()) blob = (BLOB) rs.getBlob(1); 
          13      String fileName = "c://Sunset.jpg"
          14      File f = new File(fileName); 
          15      FileInputStream fin = new FileInputStream(f); 
          16      System.out.println("file size = " + fin.available()); 
          17      pstmt = conn.prepareStatement("update imageTable set image=? where id=?"); 
          18      OutputStream out = blob.getBinaryOutputStream(); 
          19       byte[] data = new byte[(int)fin.available()]; 
          20      fin.read(data); 
          21      out.write(data); 
          22      out.close(); 
          23      fin.close(); 
          24
          25      pstmt.setBlob(1,blob); 
          26      pstmt.setString(2,"fankai"); 
          27
          28      pstmt.executeUpdate(); 
          29      pstmt.close(); 
          30
          31      conn.commit(); 
          32      conn.close(); 
          瀏覽數(shù)據(jù)庫的數(shù)據(jù),發(fā)現(xiàn)image項中數(shù)據(jù)大小與圖片大小一致,說明入庫了!
           3,blob出庫
           1   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
           2   Connection conn = DriverManager.getConnection(            "jdbc:oracle:thin:@localhost:1521:beyondduke""duke","duke");     
                    conn.setAutoCommit(
          false);
           3    Statement stmt = conn.createStatement();
           4/* 查詢BLOB對象 */
           5    ResultSet rs = stmt.executeQuery("SELECT content FROM javatest WHERE id='1001'");
           6while (rs.next()) {
           7/* 取出此BLOB對象 */    
           8    oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image");
           9 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d://Sunset.jpg"));            
                     BufferedInputStream in 
          = new BufferedInputStream(blob    .getBinaryStream());        
          10    int c;
          11while ((c = in.read()) != -1{ out.write(c);        
          12   }

          13   in.close();
          14   out.close();
          15   rs.close();
          16   stmt.close();
          17   conn.close();
          18
          檢查D盤根目錄,會發(fā)現(xiàn)Sunset.jpg文件,跟C盤下是一樣的,說明入庫出庫成功!

          posted on 2006-02-14 20:59 Vincent.Chen 閱讀(638) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 杂多县| 房产| 斗六市| 南召县| 临颍县| 宁化县| 巩留县| 栖霞市| 镇原县| 佛冈县| 新和县| 吉林省| 遂川县| 明溪县| 正阳县| 罗甸县| 临洮县| 个旧市| 尼木县| 江北区| 昌黎县| 河北省| 普安县| 博罗县| 陵川县| 体育| 屏山县| 鹿邑县| 吴桥县| 平度市| 罗城| 潮州市| 台南市| 西华县| 保山市| 东城区| 黄浦区| 华容县| 泰和县| 贵港市| 乐山市|