<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 117  文章 - 72  trackbacks - 0

    聲明:原創(chuàng)作品(標有[原]字樣)轉(zhuǎn)載時請注明出處,謝謝。

    常用鏈接

    常用設(shè)置
    常用軟件
    常用命令
     

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 155669
    • 排名 - 391

    最新評論

    [標題]:向MySQL數(shù)據(jù)庫插入Blob數(shù)據(jù)的問題
    [時間]:2009-6-3
    [摘要]:在使用Hibernate向數(shù)據(jù)庫插入Blob二進制數(shù)據(jù)時,發(fā)生如下錯誤: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
    [關(guān)鍵字]:MySQL、Blob、圖片、image、java、Hibernate、Clob、&amp;
    [環(huán)境]:5.1.34-community MySQL Community Server (GPL),Hibernate 3.2.5
    [作者]:Winty (wintys@gmail.com) http://m.tkk7.com/wintys

    [錯誤]:
        使用Hibernate向數(shù)據(jù)庫插入Blob二進制數(shù)據(jù),程序如下:
        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();
            }
        }
        發(fā)生如下錯誤:
    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的保留字段有相同的"。
        檢查了一下表中的字段名,沒有發(fā)現(xiàn)問題:
    CREATE TABLE myblobclob(
        id          VARCHAR(100) NOT NULL,
        name    VARCHAR(100),
        image   BLOB,
        info       TEXT,
        PRIMARY KEY(id)
    );

        如果把Blob相關(guān)的程序注釋了,Clob數(shù)據(jù)能夠正常寫入。原因當然出在Blob數(shù)據(jù)的寫入程序中。后來發(fā)現(xiàn),把Blob寫入的圖片數(shù)據(jù)換成文本,卻可以正常寫入。可見,是二進制數(shù)據(jù)的編碼問題。

    [解決]:
        將原來的數(shù)據(jù)連接:
    <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中的轉(zhuǎn)義字符。不然會提示錯誤:
        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相關(guān)問題 : http://fenghuang.javaeye.com/blog/363931
    posted on 2009-06-03 23:45 天堂露珠 閱讀(3983) 評論(1)  編輯  收藏 所屬分類: Error

    FeedBack:
    # re: [原]向MySQL數(shù)據(jù)庫插入Blob數(shù)據(jù)的問題 2014-05-20 17:27 moyue
    贊一個!  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲国产成人九九综合| 亚洲av永久无码精品古装片| 亚洲中文字幕人成乱码| 99re免费视频| 久久久久亚洲av无码专区喷水| 国产线视频精品免费观看视频| 亚洲精品一二三区| 88av免费观看| 亚洲国产午夜电影在线入口| 99久久免费国产香蕉麻豆| 亚洲av永久无码精品三区在线4| 美女视频黄是免费的网址| 33333在线亚洲| 成在线人永久免费视频播放| 亚洲av无码专区在线电影天堂| 国产伦精品一区二区三区免费下载 | 久草免费在线观看视频| 亚洲乱码一二三四区国产| 大地资源二在线观看免费高清| 亚洲av永久中文无码精品| 久久久无码精品亚洲日韩软件| a级毛片无码免费真人久久| 老司机亚洲精品影院| 三年片在线观看免费观看高清电影| 亚洲hairy多毛pics大全| 亚洲中文字幕视频国产| 99re这里有免费视频精品| 亚洲AV综合色区无码二区爱AV| 毛片a级三毛片免费播放| 一级毛片在线免费播放| 久久精品国产亚洲77777| 在线免费观看中文字幕| 9久热精品免费观看视频| 亚洲国产美女在线观看| 亚洲精品视频在线观看你懂的| 性xxxx视频免费播放直播| 亚洲欧美日韩中文二区| 亚洲精品无码av人在线观看| 欧美好看的免费电影在线观看| 国产精品青草视频免费播放| xxx毛茸茸的亚洲|