<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)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

    常用鏈接

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

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(lèi)(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 155593
    • 排名 - 391

    最新評(píng)論

    [標(biāo)題]:向MySQL數(shù)據(jù)庫(kù)插入Blob數(shù)據(jù)的問(wèn)題
    [時(shí)間]:2009-6-3
    [摘要]:在使用Hibernate向數(shù)據(jù)庫(kù)插入Blob二進(jìn)制數(shù)據(jù)時(shí),發(fā)生如下錯(cuò)誤: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

    [錯(cuò)誤]:
        使用Hibernate向數(shù)據(jù)庫(kù)插入Blob二進(jìn)制數(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ā)生如下錯(cuò)誤:
    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_

    [原因]:
        搜索了一下,錯(cuò)誤原因可能為:"在定義字段時(shí),不要和MYSQL的保留字段有相同的"。
        檢查了一下表中的字段名,沒(méi)有發(fā)現(xiàn)問(wè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ù)能夠正常寫(xiě)入。原因當(dāng)然出在Blob數(shù)據(jù)的寫(xiě)入程序中。后來(lái)發(fā)現(xiàn),把Blob寫(xiě)入的圖片數(shù)據(jù)換成文本,卻可以正常寫(xiě)入。可見(jiàn),是二進(jìn)制數(shù)據(jù)的編碼問(wèn)題。

    [解決]:
        將原來(lái)的數(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中,因?yàn)?amp;是XML中的轉(zhuǎn)義字符。不然會(huì)提示錯(cuò)誤:
        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)問(wèn)題 : http://fenghuang.javaeye.com/blog/363931
    posted on 2009-06-03 23:45 天堂露珠 閱讀(3982) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): Error

    FeedBack:
    # re: [原]向MySQL數(shù)據(jù)庫(kù)插入Blob數(shù)據(jù)的問(wèn)題 2014-05-20 17:27 moyue
    贊一個(gè)!  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 亚洲国产精品一区二区久久| 免费国产怡红院在线观看| 国产一区二区免费| 国内精品免费在线观看| 成人性生交大片免费看中文| 久久久久久国产a免费观看不卡| 乱爱性全过程免费视频| h片在线观看免费| 二个人看的www免费视频| 日韩精品无码免费专区午夜| 亚洲精品免费视频| 伊人久久免费视频| 亚洲黄色免费网站| 特级做A爰片毛片免费69 | 一级黄色免费毛片| 一级做α爱过程免费视频| 久久久精品视频免费观看| 久久精品免费观看| xx视频在线永久免费观看| 毛片免费观看视频| 四虎永久免费观看| 亚洲综合区小说区激情区| 国产AV无码专区亚洲AWWW| 亚洲国产精品VA在线观看麻豆| 亚洲综合日韩中文字幕v在线 | 免费一级毛片免费播放| 久久久久亚洲AV成人网| 久久亚洲精品视频| 亚洲日本香蕉视频| 亚洲avav天堂av在线网毛片| 一级毛片免费全部播放| 久久久久国产免费| 最近免费中文字幕4| 免费国产精品视频| 亚洲精品无码成人片久久| 91亚洲国产在人线播放午夜| 亚洲高清有码中文字| 一边摸一边爽一边叫床免费视频 | 亚洲欧美日韩国产精品一区| 丰满妇女做a级毛片免费观看| 久久久久久影院久久久久免费精品国产小说 |