我试图将图像添加到mysql数据库中的BLOB字段.图像尺寸将小于100kb.但是我遇到了问题,并想知道将这些数据添加到数据库的更好方法是什么?
com.mysql.jdbc.MysqlDataTruncation:数据截断:第1行的"数据"列的数据太长
PreparedStatement addImage = conn.prepareStatement("INSERT INTO Images (Width, Height, Data) VALUES (?,?,?)",Statement.RETURN_GENERATED_KEYS);
Run Code Online (Sandbox Code Playgroud)
下面是我用于将图像添加到数据库中的方法.
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);
}
Run Code Online (Sandbox Code Playgroud)
将数据字段类型更改为Mediumblob并尝试将140kb图像文件放入数据库后,我收到了不同的错误.
com.mysql.jdbc.PacketTooBigException:查询包太大
问题是我尝试将数据添加到数据库的方式.我应该采取不同的方法吗?如果是这样的话?
| 归档时间: |
|
| 查看次数: |
8708 次 |
| 最近记录: |