`

[原]向MySQL数据库插入Blob数据的问题

阅读更多
[标题]:向MySQL数据库插入Blob数据的问题
[时间]:2009-6-3
[摘要]:在使用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

[错误]:
    使用Hibernate向数据库插入Blob二进制数据,程序如下:
    public void insert() {
        User user = new User();
        Transaction tc = null;
        try{
            Session session = HibernateUtil.getSession();
            tc = session.beginTransaction();
            
            user.setName("The Name");
            
            FileInputStream fin = new FileInputStream("rc/redheart.gif");
            Blob image = Hibernate.createBlob(fin);
            user.setImage(image);
            
            File file = new File("rc/news.txt");
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            Clob info = Hibernate.createClob(br , (int)file.length());
            user.setInfo(info);
            
            session.save(user);
            
            tc.commit();
        }catch(Exception e){
            if(tc != null){
                tc.rollback();
            }
            System.err.println(e.getMessage());            
        }finally{
            HibernateUtil.closeSession();
        }
    }
    发生如下错误:
Hibernate: insert into db.myblobclob (name, image, info, id) values (?, ?, ?, ?)
00:33:45,671  WARN JDBCExceptionReporter:77 - SQL Error: 1064, SQLState: 42000
00:33:45,671 ERROR JDBCExceptionReporter:78 - 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
00:33:45,687 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at wintys.hibernate.blobclob.UserDAOBean.insert(UserDAOBean.java:41)
    at wintys.hibernate.blobclob.UserTest.main(UserTest.java:18)
Caused by: java.sql.BatchUpdateException: 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
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    ... 9 more
Could not execute JDBC batch update
Hibernate: select user0_.id as id0_, user0_.name as name0_, user0_.image as image0_, user0_.info as info0_ from db.myblobclob user0_

[原因]:
    搜索了一下,错误原因可能为:"在定义字段时,不要和MYSQL的保留字段有相同的"。
    检查了一下表中的字段名,没有发现问题:
CREATE TABLE myblobclob(
    id          VARCHAR(100) NOT NULL,
    name    VARCHAR(100),
    image   BLOB,
    info       TEXT,
    PRIMARY KEY(id)
);

    如果把Blob相关的程序注释了,Clob数据能够正常写入。原因当然出在Blob数据的写入程序中。后来发现,把Blob写入的图片数据换成文本,却可以正常写入。可见,是二进制数据的编码问题。

[解决]:
    将原来的数据连接:
<property name="connection.url">
    jdbc:mysql://localhost:3306/db
</property>
    修改成:
<property name="connection.url">
    <![CDATA[jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8]]>
</property>
    注意,在将连接字符串放到CDATA中,因为&是XML中的转义字符。不然会提示错误:
    Error parsing XML: /hibernate.cfg.xml(12) The reference to entity "characterEncoding" must end with the ';' delimiter.

    也可以直接把&修改为&amp;
    即:
    jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf-8

[参考资料]:
Mysql 中的blob相关问题 : http://fenghuang.iteye.com/blog/363931
分享到:
评论
1 楼 yoin528 2011-01-13  
useUnicode=true&amp;characterEncoding=UTF-8

在xml文件里面好像对;敏感,要转换一下。

相关推荐

    向数据库插入blob数据实例源码--在mysql中通过

    向数据库插入blob数据实例源码.rar 在mysql中通过。

    mysql oracle 插入blob数据实例源码

    向数据库插入blob数据实例源码--在mysql和oracle中通过.rar

    mysql导出工具(blob字段)

    mysql导出工具,可导出无注释插入语句,表中字段有blob类数据,插入语句前还有锁表操作语句,适合多种场景

    利用spring的jdbcTemplate处理blob、clob

    spring 中对大数据的处理,包括clob,blob的数据。比之jdbc下简便很多。

    oracle中的BLOB(照片)转换到mysql中

    从oracle中的BLOB类型字段中取出照片,转存到mysql数据库中。思路是现将oracle中的照片存储在本地文件夹,在将本地图片上传到mysql中。

    Mysql Blob的读取与插入

    Java中 Blob的插入与读取

    Mysql的longblob字段插入数据问题解决

    在使用mysql的过程中,有个问题就是mysql的优化,mysql中longblob字段在5.5版本中默认的为1M。 想改变这个问题,需要注意几点: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (2054817 &gt; ...

    Myblob:与 MySQL 数据库交换 blob(二进制大对象)-matlab开发

    MYBLOB_DEMO - 演示 MYBLOB 工具包的使用MYBLOB_OPEN - 打开与 MySQL 数据库的连接MYBLOB_COMMAND - 将 SQL 语句发送到 MySQL 数据库MYBLOB_FROM_DB - 从 MySQL 数据库中检索 blob(二进制大对象) MYBLOB_TO_DB - ...

    Mybatis 处理 CLOB、BLOB 类型数据

    Mybatis 处理 CLOB、BLOB 类型数据

    数据库表数据转为insert sql语句

    Data row to insert sql是一个小的工具软件,可以将数据库表中指定数据转换成相应的insert sql语句。目前支持的数据库类型为oracle,db2,ms sql server。 目前还有一些缺陷,还有待完善,具体如下: 1、对ms sql ...

    mysql数据库my.cnf配置文件

    # MySQL每打开一个表,都会读入一些数据到table_open_cache缓存中,当MySQL在这个缓存中找不到相应信息时,才会去磁盘上读取。默认值64 # 假定系统有200个并发连接,则需将此参数设置为200*N(N为每个连接所需的文件...

    mysql数据库的基本操作语法

    MySQL中外键的table的外键引用列可以插入数据可以为null,不参照主表的数据。 使用子查询插入数据 insert into temp(name) select name from classes; 多行插入 insert into temp values(null, ‘jack’, 22), ...

    提高MySQL中InnoDB表BLOB列的存储效率的教程

    主要介绍了提高MySQL中InnoDB表BLOB列的存储效率的教程,InnoDB的优化在MySQL的优化研究中也是一个非常热门的课题,需要的朋友可以参考下

    MySQL命令大全

    进入mysql数据库控制台, 如mysql -u root -p mysql&gt;use 数据库 然后使用source命令,后面参数为脚本文件(如这里用到的.sql) mysql&gt;source wcnc_db.sql B:使用mysqldump命令 mysqldump -u username -p dbname ...

    MYSQL

    9.4 MySQL数据库表类型 10 从 MySQL 得到最大的性能 10.1 优化概述 10.2 系统/编译时和启动参数的调节 10.2.1 编译和链接如何影响 MySQL 的速度 10.2.2 磁盘问题 10.2.2.1 为数据库和...

    MySQL 5权威指南(第3版) 中文版 下载地址

     15.6 向关联数据表插入新数据记录  15.7 处理来自HTML表单的输入数据  15.8 分页显示查询结果  15.9 处理层次化数据  15.10 速度优化  15.11 Unicode  15.12 二进制数据(BLOB)和图像  15.13 存储...

    java实现插入mysql二进制文件,blob类型,遇到问题及解决办法

    首先是数据库建立要准备的: 我们要把放置二进制字段设置为Blob类型,根据文件的大小选择合适的Blob类型,一下是各个Blob类型所能容纳二进制文件的大小 MySQL的四种BLOB类型 类型 大小(单位:字节) TinyBlob 最大 ...

    图片插入数据库(数据库下载图片到本地)

    Mysql中可以存储大文件数据,一般使用的BLOB对象。如图片,视频等等。  BLOB是一个二进制大对象,可以容纳可变数量的数据。因为是二进制对象,所以与编码方式无关。有4种BLOB类型:TINYBLOB、BLOB、MEDIUMBLOB和...

    MySQL5 权威指南第3版中文版_part1

     15.6 向关联数据表插入新数据记录  15.7 处理来自HTML表单的输入数据  15.8 分页显示查询结果  15.9 处理层次化数据  15.10 速度优化  15.11 Unicode  15.12 二进制数据(BLOB)和图像  15.13 存储过程  ...

Global site tag (gtag.js) - Google Analytics