konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks
          如果將要檔案寫入資料庫,您可以在欄位上使用BLOB或CLOB資料型態(tài)BLOB全名Binary Large Object,用於儲存大量的二進位資料,CLOB全名Character Large Object,用於儲存大量的文字資料。

          在JDBC中也提供了Blob與Clob兩個類別分別代表BLOB與CLOB資料,JDBC並沒有提供直接存入BLOB或CLOB的對應介面(像是setBlob()、setClob()等),但您可以使用PreparedStatement的setBinaryStream()、 setObject()、setAsciiStream()、setUnicodeStream()等方法來代替,例如我們可以如下取得一個檔案,並將之存入資料庫中:
          // 取得檔案
          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資料,您可以如下進行:
          Blob blob = result.getBlob(2);  // 取得BLOB
          Clob clob = result.getClob(2)  // 取得CLOB 
           

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

          下面這個程式示範基本的檔案存入資料庫並取出另存新檔:
          BLOBCLOBDemo.java

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.sql.Blob;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.sql.Statement;

          /**
           * @author Administrator
           * 
           * TODO To change the template for this generated type comment go to Window -
           * Preferences - Java - Code Style - Code Templates
           
          */

          public class BLOBCLOBDemo {
              
          public static void main(String[] args) {
                  String driver 
          = "com.mysql.jdbc.Driver";
                  String url 
          = "jdbc:mysql//localhost:3306/upload?"
                          
          + "useUnicode=true&characterEncoding=gbk";
                  String user 
          = "root";
                  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.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("找不到驅(qū)動程式");
                      e.printStackTrace();
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

              }

          }

          posted on 2005-08-10 22:47 konhon 優(yōu)華 閱讀(450) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 万全县| 蒲江县| 通榆县| 云霄县| 晋宁县| 昌图县| 鄂托克前旗| 曲松县| 柳林县| 论坛| 普格县| 于田县| 青川县| 会泽县| 舞钢市| 文水县| 察雅县| 洪洞县| 宁晋县| 马山县| 芮城县| 灵川县| 临西县| 高唐县| 乡宁县| 新津县| 松江区| 普定县| 太仆寺旗| 宾川县| 体育| 沁源县| 连江县| 新乡市| 湘阴县| 浦东新区| 翁牛特旗| 太仓市| 通渭县| 东乡族自治县| 舟山市|