使用PreparedStatement向資料表中插入、修改、刪除、獲取Blob型別的資料

TZQ_DO_Dreamer發表於2014-09-07

Blob介紹

BLOB型別的欄位用於儲存二進位制資料

MySQL中,BLOB是個型別系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,這幾個型別之間的唯一區別是在儲存檔案的最大大小上不同。

MySQL的四種BLOB型別
型別       大小(單位:位元組)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G

Oracle LOB介紹

    LOB,即Large Objects(大物件),是用來儲存大量的二進位制和文字資料的一種資料型別(一個LOB欄位可儲存可多達4GB的資料)。
LOB 分為兩種型別:內部LOB和外部LOB。

內部LOB將資料以位元組流的形式儲存在資料庫的內部。因而,內部LOB的許多操作都可以參與事務,也可以像處理普通資料一樣對其進行備份和恢復操作。Oracle支援三種型別的內部LOB:
  1. BLOB(二進位制資料)  
  2. CLOB(單位元組字元資料) 
  3. NCLOB(多位元組字元資料)。
CLOB和NCLOB型別適用於儲存超長的文字資料,BLOB欄位適用於儲存大量的二進位制資料,如影象、視訊、音訊,檔案等。
目前只支援一種外部LOB型別,即BFILE型別。在資料庫內,該型別僅儲存資料在作業系統中的位置資訊,而資料的實體以外部檔案的形式存在於作業系統的檔案系統中。因而,該型別所表示的資料是隻讀的,不參與事務。該型別可幫助使用者管理大量的由外部程式訪問的檔案。


程式示例:

package tan;
import java. io.*;
import java. sql.*;
import java. text.SimpleDateFormat;
import org. junit.Test;
public class TestJDBC {
    

    //如何獲取資料表中的 blob型別的變數

    @Test
    public void testBlob2() {
        Connection conn =null;
        PreparedStatement ps =null;
        ResultSet rs =null;
        InputStream is =null;
        FileOutputStream fos =null;
         try {
            conn =JDBCUtils .getConnection ();
            String sql ="select id,name,email,birth,photo from customers where id=?";
            ps =conn .prepareStatement (sql );
            ps .setInt (1, 16);//獲取id=16的資料
            rs =ps .executeQuery ();
             if(rs.next()){
                 int id =rs .getInt (1);
                String name =rs .getString (2);
                String email =rs .getString (3);
                Date birth =rs .getDate (4);
                Blob photo =rs .getBlob (5);
                is =photo .getBinaryStream ();//利用輸入流來讀取資料庫中的二進位制檔案
                fos =new FileOutputStream (new File("girl.png"));//輸出到本地
                
                 byte []buf =new byte[ 100];
                 int len =0;
                 while((len=is.read(buf))!=-1 ){
                    fos .write (buf ,0, len);
                 }
                Customer cust =new Customer (id , name , email, birth);//將非blob型別封裝成物件輸出
                System .out .println (cust );
             }
            
            
         } catch (Exception e ) {
            
         }finally{
            JDBCUtils .close (rs , ps , conn );//記得要關閉流
             if(fos != null){
                 try {
                    fos .close ();
                 } catch (IOException e ) {
                    e .printStackTrace ();
                 }
                
             }
             if(is != null){
                 try {
                    is .close ();
                 } catch (IOException e ) {
                     // TODO Auto-generated catch block
                    e .printStackTrace ();
                 }
                
             }
         }
    }
    

    // 修改資料表包含圖片資訊的資料

    @Test
    public void testBlob1() {
        Connection conn =null;
        PreparedStatement ps =null;
        FileInputStream fis =null;
         try {
            conn = JDBCUtils .getConnection ();
            String sql = "update customers set photo = ? where id = ?";
            ps = conn .prepareStatement (sql );
             // 填充佔位符
            fis = new FileInputStream (new File("1.png" ));
            ps .setBlob (1, fis);
            ps .setInt (2, 16 );
            ps .execute ();
            
         } catch (Exception e ) {
            e .printStackTrace ();
         }finally{
            JDBCUtils .close (null, ps, conn);
             try {
                fis .close ();
             } catch (IOException e ) {
                e .printStackTrace ();
             }
         }
        
    }

    // 向資料表中插入一條包含圖片資訊的資料

    @Test
    public void testBlob() {
        Connection conn =null;
        PreparedStatement ps =null;
        FileInputStream fis =null;
         try {
            conn =JDBCUtils .getConnection ();
            String sql ="insert into customers(name,email,birth,photo)values(?,?,?,?)";
            ps =conn .prepareStatement (sql );
            ps .setString (1, " zhengqiang");
            ps .setString (2, "beipiao@123.com" );
             //日期轉換
            String date ="1991-11-13";
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            java .util .Date d =sdf .parse (date );

            ps .setDate (3, new Date(d.getTime()));

            //利用檔案輸入流寫入
            fis =new FileInputStream (new File("66.jpg" ));
            ps .setBlob (4, fis);
             //執行預編譯語句
            ps .execute ();
            
         } catch (Exception e ) {
            e .printStackTrace ();
         }finally{
            JDBCUtils .close (null, ps, conn);
            try {
                fis .close ();
             } catch (IOException e ) {
                e .printStackTrace ();
             }
         }
        
    }
    
}

相關文章