天天看點

MySQL資料庫操作Blob類型字段一、MySQL BLOB類型二、用PreparedStatement操作Blob類型的資料

一、MySQL

BLOB

類型

  • MySQL中,

    BLOB

    是一個二進制大型對象,是一個可以存儲大量資料的容器,它能容納不同大小的資料。
  • 插入

    BLOB

    類型的資料必須使用

    PreparedStatement

    ,因為

    BLOB

    類型的資料無法使用字元串拼接寫的。
  • MySQL的四種

    BLOB

    類型(除了在存儲的最大資訊量上不同外,他們是等同的)。
    MySQL資料庫操作Blob類型字段一、MySQL BLOB類型二、用PreparedStatement操作Blob類型的資料
  • 實際使用中根據需要存入的資料大小定義不同的

    BLOB

    類型。
  • 如果存儲的檔案過大,資料庫的性能會下降。
  • 如果在指定了相關的

    Blob

    類型以後,還報錯

    xxx too large

    ,那麼在mysql的安裝目錄下,找

    my.ini

    檔案加上如下的配置參數:

    max_allowed_packet=16M

    。同時注意:修改了

    my.ini

    檔案之後,需要重新啟動mysql服務。

二、用PreparedStatement操作Blob類型的資料

插入

Blob

類型的字段資料

//使用PreparedStatement操作Blob類型的資料
public class BlobTest {

    //向資料表customers中插入Blob類型的字段資料
    @Test
    public void testInsert(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();

            String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";

            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setObject(1,"李四");
            preparedStatement.setObject(2,"[email protected]");
            preparedStatement.setObject(3,"2021-04-03");

            FileInputStream fileInputStream = new FileInputStream(new File("圖檔.png"));
            preparedStatement.setBlob(4,fileInputStream);

            preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection,preparedStatement);
        }

    }
}

           

讀取

Blob

類型的字段資料

//讀取Blob類型字段資料
    @Test
    public void testQuery(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        InputStream photoBinaryStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            connection = JDBCUtils.getConnection();

            String sql = "select id,name,email,birth,photo from customers where id = ?";

            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setInt(1,22);

            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                Date birth = resultSet.getDate("birth");
    
                Customer customer = new Customer(id, name, email, birth);
    
                System.out.println(customer);
                
                //将Blob類型的字段資料下載下傳下來,以檔案的方式儲存在本地
                Blob photo = resultSet.getBlob("photo");
                photoBinaryStream = photo.getBinaryStream();
                fileOutputStream = new FileOutputStream(new File("photo1.png"));
                byte[] buffer = new byte[1024];
                int len;
                while((len = photoBinaryStream.read(buffer)) != -1){
                    fileOutputStream.write(buffer,0,len);
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (photoBinaryStream != null)
                    photoBinaryStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            JDBCUtils.closeResource(connection,preparedStatement,resultSet);

        }
        
    }