Java BLOB 資料的插入與讀取 操作

來源:互聯網
上載者:User

標籤:

package com.lw.database;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * CREATE: *         CREATE TABLE IDCard ( id char(18),pic BLOB); * @author liuwei * Time: 2015年5月25日 下午3:22:26 */public class LOBTest {    protected static final String DEFAULT_URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";    protected static final String DRIVER_NAME = "com.mysql.jdbc.Driver";        private Connection connection = null;        public LOBTest() throws ClassNotFoundException, SQLException {        Class.forName(DRIVER_NAME);        connection = DriverManager.getConnection(DEFAULT_URL, "user", "password");    }        public void insert(String id,String path) throws SQLException, IOException {        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO IDCard VALUES (?,?)");        preparedStatement.setString(1, id);        FileInputStream fileInputStream = new FileInputStream(path);        preparedStatement.setBlob(2, fileInputStream,fileInputStream.available());        preparedStatement.execute();    }        public void get(String id) throws SQLException, IOException {        PreparedStatement preparedStatement = connection.prepareStatement("SELECT pic FROM IDCard WHERE id = ?");        preparedStatement.setString(1, id);        ResultSet results = preparedStatement.executeQuery();        while(results.next()) {            FileOutputStream outputStream = new FileOutputStream("/Users/liuwei/temp.png");            InputStream inputStream = results.getBinaryStream(1);            int num = -1;            while((num=inputStream.read())!=-1) {                outputStream.write(num);            }            outputStream.flush();            inputStream.close();            outputStream.close();        }    }        public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {        LOBTest test = new LOBTest();        test.insert("78907656784323", "/Users/liuwei/Documents/bt_next_nor.png");        test.get("78907656784323");    }}

 

注意:

  MySQL的四種BLOB類型 

    類型  大小(單位:位元組) 

      TinyBlob  最大 255B

        Blob  最大 65K 

        MediumBlob  最大 16M 

      LongBlob  最大 4G 

插入映像的時候,注意像大小,映像超過該類型所能容納的最大位元組的時候,會報錯

 

Java BLOB 資料的插入與讀取 操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.