Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mysql

jdbc

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?

like image 575
user3230425 Avatar asked Feb 03 '14 08:02

user3230425


People also ask

How do you solve data too long for a column?

That error message means you are inserting a value that is greater than the defined maximum size of the column. The solution to resolve this error is to update the table and change the column size.

What is Data truncated for column?

That error means the data is too large for the data type of the MySQL table column.

What is meant by data truncation?

In databases and computer networking data truncation occurs when data or a data stream (such as a file) is stored in a location too short to hold its entire length.


2 Answers

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.

like image 105
Aniket Kulkarni Avatar answered Oct 02 '22 23:10

Aniket Kulkarni


Use data type LONGBLOB instead of BLOB in your database table.

like image 24
Saurabh Kachhia Avatar answered Oct 03 '22 01:10

Saurabh Kachhia