mysql中寫入blob類型的方法

1、BLOB類型
1、BLOB類型介紹
(1)BLOB類型的含義
BLOB (binary large object),用來存儲二進制大對象的字段類型。
BLOB往往是一個大文件,典型的BLOB是一張圖片、一個聲音或一個視頻文件,由於它們的尺寸,必須使用特殊的方式來處理(例如:上傳、下載或者存放到一個數據庫)。
處理BLOB的主要思想就是讓文件處理器(如數據庫管理器)不去理會文件是什麼,而是關心如何去處理它。但也有專家強調,這種處理大數據對象的方法是把雙刃劍,它有可能引發一些問題,如存儲的二進制文件過大,會使數據庫的性能下降。

(2)BLOB類型系列
MySQL中,BLOB是個類型系列,共包括四種BLOB類型:TinyBlob、Blob、MediumBlob、LongBlob,這幾個類型之間的唯一區別是在存儲文件的最大尺寸不同。

字段類型 最大長度(字節)   存儲需求
TinyBlob 255   值的長度加上用於記錄長度的1個字節(8位)
Blob 65K值的長度加上用於記錄長度的2個字節(16位)
MediumBlob 16M值的長度加上用於記錄長度的3個字節(24位)
LongBlob 4G   值的長度加上用於記錄長度的4個字節(32位)


二進制文件的處理:
 1.創建blob。
 2.向blob中寫入字節。
 3.將blob寫到數據庫


1、插入Blob類型的數據
      插入Blob類型的數據時,需要注意必須要用PreparedStatement,因爲Blob類型的數據是不能夠用字符串來拼的,在傳入了SQL語句後,就需要去調用PreparedStatement對象中的setBlob(int index , InputStream in)方法來設置傳入的的參數,其中index表示Blob類型的數據所對應的佔位符(?)的位置,而InputStream類型的in表示被插入文件的節點流。
2、讀取Blob類型的數據
      讀取Blob類型相對來說比較容易,當獲取了查詢的結果集之後,使用getBlob()方法讀取到Blob對象,然後調用Blob的getBinaryStream()方法得到輸入流,再使用IO操作進行文件的寫入操作即可。


代碼如下
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
public class TestBlob {
@Test
    public void getBlob(){//讀取Blob數據
       Connection con = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       try {
             con = JDBCTools.getConnection();
             String sql = "SELECT id,name,age,picture FROM animal WHERE id=5";
             ps = con.prepareStatement(sql);
             rs = ps.executeQuery();
             if(rs.next()){
                    int id = rs.getInt(1);
                    String name = rs.getString(2);
                    int age = rs.getInt(3);
                    
                    Blob picture = rs.getBlob(4);//得到Blob對象
                    //開始讀入文件
                    InputStream in = picture.getBinaryStream();
                    OutputStream out = new FileOutputStream("cat.png");
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while((len = in.read(buffer)) != -1){
                           out.write(buffer, 0, len);
                    }
             }
       } catch (Exception e) {
        e.printStackTrace();
       }
}
@Test
    public void insertBlob(){//插入Blob
       Connection con = null;
       PreparedStatement ps = null;
       try {
             con = JDBCTools.getConnection();
             String sql = "INSERT INTO animal(name,age,picture) VALUES(?,?,?)";
             ps = con.prepareStatement(sql);
             ps.setString(1, "TheCat");
             ps.setInt(2, 8);
             InputStream in = new FileInputStream("J:/test1/TomCat.png");//生成被插入文件的節點流
             //設置Blob
             ps.setBlob(3, in);
             
             ps.executeUpdate();
       } catch (Exception e) {
        e.printStackTrace();
       }finally{
             JDBCTools.release(con, ps);
       }
}
class JDBCTools {//JDBC工具類  用來建立連接和釋放連接
       public static Connection getConnection() throws Exception{//連接數據庫
             String driverClass = null;
             String url = null;
             String user = null;
             String password = null;
             
             Properties properties = new Properties();
             
             InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties");
             properties.load(in);
             
             driverClass = properties.getProperty("driver");
             url = properties.getProperty("jdbcurl");
             user = properties.getProperty("user");
             password = properties.getProperty("password");
             Class.forName(driverClass);
             return DriverManager.getConnection(url, user, password);
       }
       public static void release(Connection con , Statement state){//關閉數據庫連接
             if(state != null){
                    try {
                           state.close();
                    } catch (SQLException e) {
                           e.printStackTrace();
                    }
             }
             if(con != null){
                    try {
                           con.close();
                    } catch (SQLException e) {
                           e.printStackTrace();
                    }
             }
             
       }
       public static void release(ResultSet rs , Connection con , Statement state){//關閉數據庫連接
             if(rs != null)
             {
                    try {
                           rs.close();
                    } catch (SQLException e) {
                           e.printStackTrace();
                    }
             }
             if(state != null){
                    try {
                           state.close();
                    } catch (SQLException e) {
                           e.printStackTrace();
                    }
             }
             if(con != null){
                    try {
                           con.close();
                    } catch (SQLException e) {
                           e.printStackTrace();
                    }
             }
       }
}
}
public class Review {
       public  Connection getConnection() throws Exception{//連接數據庫
             String driverClass = null;
             String url = null;
             String user = null;
             String password = null;
             
             Properties properties = new Properties();
             
             InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties");
             
             properties.load(in);
             
             driverClass = properties.getProperty("driver");
             url = properties.getProperty("jdbcurl");
             user = properties.getProperty("user");
             password = properties.getProperty("password");
             Class.forName(driverClass);
             return DriverManager.getConnection(url, user, password);
       }
       
}

發佈了36 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章