Java mysql blob 數據讀寫操作


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 fhadmin
 * from www.fhadmin.cn
 */
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 

插入圖像的時候,注意下圖像大小,圖像超過該類型所能容納的最大字節的時候,會報錯

工作流模塊--------------------------------------------------------------------------www.fhadmin.cn
1.模型管理    :web在線流程設計器、預覽流程xml、導出xml、部署流程
2.流程管理    :導入導出流程資源文件、查看流程圖、根據流程實例反射出流程模型、激活掛起
3.運行中流程:查看流程信息、當前任務節點、當前流程圖、作廢暫停流程、指派待辦人
4.歷史的流程:查看流程信息、流程用時、流程狀態、查看任務發起人信息
5.待辦任務   :查看本人個人任務以及本角色下的任務、辦理、駁回、作廢、指派一下代理人
6.已辦任務   :查看自己辦理過的任務以及流程信息、流程圖、流程狀態(作廢 駁回 正常完成)

注:當辦理完當前任務時,下一任務待辦人會即時通訊收到新任務消息提醒,當作廢和完結任務時,
       任務發起人會收到站內信消息通知


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM