我试图将照片插入到MySQL表的BLOB列中,但出现异常:
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(); }
我该如何解决这个问题?
您试图插入的数据大于列允许的数据logo。
logo
根据需要使用以下数据类型
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以避免此异常。
LONGBLOB