我正在尝试将图像添加到 mysql 数据库中的 BLOB 字段。图像大小将小于 100kb。但是我遇到了问题,想知道将这些数据添加到数据库的更好方法是什么?
com.mysql.jdbc.MysqlDataTruncation:数据截断:第 1 行的“数据”列的数据太长
PreparedStatement addImage = conn.prepareStatement("INSERT INTO Images (Width, Height, Data) VALUES (?,?,?)",Statement.RETURN_GENERATED_KEYS);
以下是我用来将图像添加到数据库的方法。
public int addImage(Image image) throws SQLException, IllegalArgumentException
{
this.addImage.clearParameters();
byte[] imageData = ImageConverter.convertToBytes(image);
int width = image.getWidth(null);
int height = image.getHeight(null);
if (width == -1 || height == -1)
{
throw new IllegalArgumentException("You must load the image first.");
}
this.addImage.setInt(1, width);
this.addImage.setInt(2, height);
this.addImage.setBytes(3, imageData);
this.addImage.executeUpdate();
ResultSet rs = this.addImage.getGeneratedKeys();
rs.next();
return rs.getInt(1);
}
表的 SQL 定义 http://pastebin.com/m3cd428c2
将数据字段类型更改为 Mediumblob 并尝试将 140kb 图像文件放入数据库后,我收到了不同的错误。
com.mysql.jdbc.PacketTooBigException:查询数据包太大
问题是我尝试将数据添加到数据库的方式吗?我应该采取不同的方法吗?如果可以的话走哪条路?