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?
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.
That error means the data is too large for the data type of the MySQL table column.
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.
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.
Use data type LONGBLOB
instead of BLOB
in your database table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With