Mysql 中的blob相关问题
来源:互联网 发布:苹果手机记账软件 编辑:程序博客网 时间:2024/04/29 13:29
在数据库中存文件可以保证数据的安全,以及服务器的安全
MySQL中,BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据。BLOB类型实际是个类型系列(TinyBlob、Blob、MediumBlob、LongBlob),除了在存储的最大信息量上不同外,他们是等同的。
MySQL的四种BLOB类型
实际使用中根据需要存入的数据大小定义不同的BLOB类型。
需要注意的是:如果你存储的文件过大,数据库的性能会下降很多。
二、mysql中的blob存取
create table Dish {
blob photo;
};
下面是从数据库里写的方法:
下面是从数据库里读的方法:
1.BufferedInputStream inputimg = null;
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select from Dish where id = 11");
if(rs.next()){
java.sql.Blobblob = (java.sql.Blob)rs.getBlob("photo");
input = new BufferedInputStream(blob.getBinaryStream);
}
BufferedImage image = null;
image = javax.imageio.ImageIo.read(input);
ServlerOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
input.close();
}catch(Exception e) {
e.printStackTrace();
}
2.
if(rs.next()){
}
3.
if(rs.next()){
}
三、max_allowed_packet参数设置
往数据库中存储较大的文件是出现如下错误:
- <strong>java.lang.IllegalArgumentException
: 1048576Packet is larger than max_allowed_packet from server configuration of bytes</strong>
java.lang.IllegalArgumentException: Packet is larger than max_allowed_packet from server configuration of 1048576 bytes
这是因为存入的文件大于mysql默认的
解决办法:在mysql安装目录下的my.ini文件中的最后一行添加
- max_allowed_packet
= 10M(也可以设置自己需要的大小)。
max_allowed_packet = 10M(也可以设置自己需要的大小)。
四
利用数据库存储大量文件时,查询效率就会变得很低。
在表的设计上,我们可以选择吧文件的相关信息存在一个表中fileInfo,而吧文件内容存在另一个表中 fileContent,fileContent中有一个指向fileInfo的外键。这样,查询的时候只需要访问fileInfo,只有当要访问某个文件具体内容的时候才访问fileContent表。分表存储,能够显著提高查询速度。
-----------------------------
向MySQL数据库插入Blob数据的问题
[摘要]:在使用Hibernate向数据库插入Blob二进制数据时,发生如下错误:SQL Error: 1064, SQLState: 42000 。You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??^5b??08""199G?"0Px8=?ü??Y??ó??l%P?[
¨???ó`-??F????:???S?a?@??Zu??' at line 1
[关键字]:MySQL、Blob、图片、image、java、Hibernate、Clob、&
[环境]:5.1.34-community MySQL Community Server (GPL),Hibernate 3.2.5
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[错误]: