数据截断:数据太长,导致第1行的“徽标"列 [英] Data truncation: Data too long for column 'logo' at row 1

查看:297
本文介绍了数据截断:数据太长,导致第1行的“徽标"列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将照片插入MySQL表的BLOB列中,但出现异常:

I am trying to insert a photo into a BLOB column of a MySQL table, and I get an exception:

Data too long for column 'logo' at row 1. 

这是JDBC:

    int idRestaurant = 42;
    String restoname=  "test";
    String restostatus=  "test";
    InputStream fileContent = getUploadedFile();
    int fileSize = getUploadedFileSize();

    Class.forName("com.mysql.jdbc.Driver");
    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/resto" , "root" , "" )) {
        PreparedStatement ps = conn.prepareStatement("insert into restaurants (idRestaurant, restaurantName, status, logo) values(?,?,?,?)");
        ps.setInt(1, idRestaurant);
        ps.setString(2, restoname);
        ps.setString(3, restostatus);
        ps.setBinaryStream(4, fileContent, fileSize);
        ps.executeUpdate();
        conn.commit();
    }

我该如何解决这个问题?

How do I solve this problem?

推荐答案

您正试图插入大于列logo所允许的数据.

You are trying to insert data that is larger than allowed for the column logo.

根据需要使用以下数据类型

Use following data types as per your need

TINYBLOB   :     maximum length of 255 bytes  
BLOB       :     maximum length of 65,535 bytes  
MEDIUMBLOB :     maximum length of 16,777,215 bytes  
LONGBLOB   :     maximum length of 4,294,967,295 bytes  

使用LONGBLOB避免此异常.

这篇关于数据截断:数据太长,导致第1行的“徽标"列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆