java讀取圖像文件存入oracle中blob字段源代碼

          最近因為要做點東西很少寫文章了。
          尤其是技術類的文章。
          在網(wǎng)上看了很多關于Java對blob字段的操作。
          自己也嘗試著寫了個,
          自己也修改了部分。
          代碼寫得很亂。

          數(shù)據(jù)庫:oracle 10G  XE
          數(shù)據(jù)源驅(qū)動:jdbc12.jar
          文件名:WriteBlob
          數(shù)據(jù)庫中建立一個為clobtest的表,內(nèi)有兩個字段,name (varchar2(20)),content(blob)。
           1package dbdriver;
           2
           3/**
           4 * 2008-09-28
           5 * @author duduli
           6 * email: lxyzj2000@gmail.com
           7 */

           8import java.sql.*;
           9import java.io.*;
          10import oracle.sql.*;
          11
          12public class WriteBlob {
          13
          14    public static void main(String[] args) {
          15        try {
          16            String fName2 = "";
          17            String fileName = "E:\\jianxin.bmp";
          18//E盤下游個jianxin.bmp的圖像文件
          19            File f = new File(fileName);
          20            String fName = f.getName();  
          21            int i = fName.lastIndexOf('.');   
          22            if (i > 0 && i < fName.length()-1){   
          23                   fName2 = fName.substring(0,i);   
          24                }
             
          25            System.out.println(fName2);
          26//獲得文件名,出去后綴的文件名。
          27            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
          28            Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE""system""duduli   ");
          29            conn.setAutoCommit(false);
          30            BLOB blob = null;
          31            PreparedStatement pstmt = conn.prepareStatement("insert into blobtest(name,content) values(?,empty_blob())");
          32            pstmt.setString(1, fName2);
          33            pstmt.executeUpdate();
          34            pstmt.close();
          35            pstmt = conn.prepareStatement("select content from blobtest where name= ? for update");
          36            pstmt.setString(1, fName2);
          37            ResultSet rset = pstmt.executeQuery();
          38            if (rset.next()) {
          39                blob = (BLOB) rset.getBlob(1);
          40            }

          41
          42            FileInputStream fin = new FileInputStream(f);
          43            System.out.println("file size = " + fin.available());
          44            pstmt = conn.prepareStatement("update blobtest set content=? where name=?");
          45            OutputStream out = blob.getBinaryOutputStream();
          46            byte[] data = new byte[(int) fin.available()];
          47            fin.read(data);
          48            out.write(data);
          49            fin.close();
          50            out.close();
          51            pstmt.setBlob(1, blob);
          52            pstmt.setString(2, fName2);
          53            pstmt.executeUpdate();
          54            pstmt.close();
          55            conn.commit();
          56            conn.close();
          57                }
           catch (SQLException e) {
          58                    System.err.println(e.getMessage());
          59                    e.printStackTrace();
          60                }
           catch (IOException e) {
          61                    System.err.println(e.getMessage());
          62                }

          63    }

          64}

          posted on 2008-10-02 21:08 duduli 閱讀(5815) 評論(3)  編輯  收藏 所屬分類: java

          評論

          # re: java讀取圖像文件存入oracle中blob字段源代碼[未登錄] 2008-10-03 18:35 java宅男

          你好,可以給我留下你的聯(lián)系方式嗎?
          我的Email
          chenglu@yeah.net~
          跟著高手學學  回復  更多評論   

          # re: java讀取圖像文件存入oracle中blob字段源代碼 2008-10-03 21:53 免費小說

          感覺滿復雜。。。。  回復  更多評論   

          # re: java讀取圖像文件存入oracle中blob字段源代碼[未登錄] 2008-10-04 19:30 aaa

          麻煩您能再寫個從數(shù)據(jù)庫獲得圖片的帖子  回復  更多評論   

          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統(tǒng)計

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地攤兒

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          @duduli
          主站蜘蛛池模板: 汾西县| 大名县| 西昌市| 随州市| 丰宁| 昌图县| 化德县| 山东省| 南投县| 繁昌县| 闽侯县| 邵阳市| 时尚| 银川市| 胶州市| 维西| 固始县| 勃利县| 云梦县| 静海县| 镇赉县| 南岸区| 沾益县| 玛曲县| 团风县| 海口市| 丹凤县| 拜城县| 循化| 九江县| 龙川县| 千阳县| 锡林郭勒盟| 平顶山市| 同心县| 雅安市| 孟津县| 洛阳市| 慈溪市| 宁武县| 玉林市|