sooxin-JAVA

          從JAVA讀取BLOB

          如果將要檔案寫入資料庫,您可以在欄位上使用BLOB或CLOB資料型態,BLOB全名Binary Large Object,用於儲存大量的二進位資料,CLOB全名Character Large Object,用於儲存大量的文字資料。

          在JDBC中也提供了Blob與Clob兩個類別分別代表BLOB與CLOB資料,JDBC並沒有提供直接存入BLOB或CLOB的對應介面(像是setBlob()、setClob()等),但我們可以使用PreparedStatement的setBinaryStream()、setObject()、setAsciiStream()、setUnicodeStream()等方法來代替,例如我們可以如下取得一個檔案,並將之存入資料庫中:
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
                      // 取得檔案
                      File file = new File("./logo_phpbb.jpg");
                      int length = (int) file.length();
                      InputStream fin = new FileInputStream(file);
                      // 填入資料庫
                      PreparedStatement pstmt = conn.prepareStatement(
                      "INSERT INTO files VALUES(?, ?)");
                       
                      pstmt.setString(1, "米小國Logo");
                      pstmt.setBinaryStream (2, fin, length);
                       
                      pstmt.executeUpdate();
                      pstmt.clearParameters();
                      pstmt.close();
                      fin.close();
                      


          如果要從資料庫中取得BLOB或CLOB資料,我們可以如下進行:
          1
          2
          Blob blob = result.getBlob(2);  // 取得BLOB
                      Clob clob = result.getClob(2)  // 取得CLOB 
                      


          Blob擁有getBinaryStream()、getBytes()等方法,可以取得二進位串流或byte等資料,同樣的,Clob擁有getCharacterStream()、getSubString()等方法,可以取得字元串流或子字串等資料,您可以查看API文件來獲得更詳細的訊息。

          下面這個程式示範基本的檔案存入資料庫並取出另存新檔:
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          45
          46
          47
          48
          49
          50
          51
          52
          53
          54
          55
          56
          57
          import java.io.*;
                      import java.sql.*;
                      public class DBTest {
                      public static void main(String[] args) {
                      String driver = "com.mysql.jdbc.Driver";
                      String url = "jdbc:mysql://localhost:3306/upload?useUnicode=true&characterEncoding=Big5";
                      String user = "caterpillar";
                      String password = "123456";
                      try {
                      Class.forName(driver);
                      Connection conn = DriverManager.getConnection(url, user, password);
                       
                      // 取得檔案
                      File file = new File("./logo_phpbb.jpg");
                      int length = (int) file.length();
                      InputStream fin = new FileInputStream(file);
                       
                      // 填入資料庫
                      PreparedStatement pstmt = conn.prepareStatement(
                      "INSERT INTO files VALUES(?, ?)");
                      pstmt.setString(1, "米小國Logo");
                      pstmt.setBinaryStream (2, fin, length);
                      pstmt.executeUpdate();
                      pstmt.clearParameters();
                      pstmt.close();
                      fin.close();
                       
                      // 從資料庫取出檔案
                      Statement stmt = conn.createStatement();
                      ResultSet result = stmt.executeQuery("SELECT * FROM files");
                      result.next();
                      String description = result.getString(1);
                      Blob blob = result.getBlob(2);
                       
                      // 寫入檔案
                      System.out.println("檔案描述:" + description);
                      FileOutputStream fout = new FileOutputStream("./logo_phpbb_2.jpg");
                      fout.write(blob.getBytes(1, (int)blob.length()));
                      fout.flush();
                      fout.close();
                      stmt.close();
                      conn.close();
                      }
                      catch(ClassNotFoundException e) {
                      System.out.println("找不到驅動程式");
                      e.printStackTrace();
                      }
                      catch(SQLException e) {
                      e.printStackTrace();
                      }
                      catch(IOException e) {
                      e.printStackTrace();
                      }
                      }
                      }
                      


          posted on 2007-04-26 16:13 sooxin 閱讀(4973) 評論(0)  編輯  收藏 所屬分類: JAVA

          主站蜘蛛池模板: 织金县| 镇康县| 闽清县| 六安市| 桃园市| 即墨市| 开平市| 肥城市| 从化市| 图木舒克市| 年辖:市辖区| 衡阳县| 屏东市| 北流市| 腾冲县| 皋兰县| 文山县| 商水县| 辰溪县| 阜康市| 丽江市| 赤壁市| 罗甸县| 濮阳县| 台州市| 左权县| 中西区| 斗六市| 崇左市| 太仓市| 类乌齐县| 琼结县| 宝山区| 马公市| 张家界市| 蒲城县| 灌云县| 周口市| 瑞丽市| 青铜峡市| 丽水市|