【发布时间】:2010-10-12 01:55:27
【问题描述】:
我正在尝试将图像添加到 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);
}
在将数据字段类型更改为 Mediumblob 并尝试将 140kb 图像文件放入数据库后,我收到了另一个错误。
com.mysql.jdbc.PacketTooBigException: 查询包太大
问题是我尝试将数据添加到数据库的方式。我应该采取不同的方法吗?如果是这样,那是什么方式?
【问题讨论】:
-
发布您尝试添加图像的表的定义可能值得。