Search code examples
javamysqljdbc

Data truncation: Data too long for column 'logo' at row 1


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. 

Here is the 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?


Solution

  • 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  
    

    Use LONGBLOB to avoid this exception.