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

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

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

    隨筆 - 117  文章 - 72  trackbacks - 0

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

    常用鏈接

    常用設置
    常用軟件
    常用命令
     

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 155536
    • 排名 - 390

    最新評論

    [標題]:向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、&amp;
    [環境]:5.1.34-community MySQL Community Server (GPL),Hibernate 3.2.5
    [作者]:Winty (wintys@gmail.com) http://www.tkk7.com/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.javaeye.com/blog/363931
    posted on 2009-06-03 23:45 天堂露珠 閱讀(3982) 評論(1)  編輯  收藏 所屬分類: Error

    FeedBack:
    # re: [原]向MySQL數據庫插入Blob數據的問題 2014-05-20 17:27 moyue
    贊一個!  回復  更多評論
      
    主站蜘蛛池模板: 亚洲人成在线播放| 男女男精品网站免费观看| 亚洲日韩中文字幕天堂不卡| 亚洲日韩一区二区一无码| 未满十八私人高清免费影院| 免费无遮挡无码永久视频| 亚洲AV永久无码精品一区二区国产 | 国产乱子伦片免费观看中字| 亚洲成AV人片天堂网无码| 亚洲精品无码永久在线观看男男 | 男女超爽视频免费播放| **aaaaa毛片免费| 国产成人毛片亚洲精品| 久久夜色精品国产噜噜亚洲a| 91成人免费福利网站在线| 国产精品另类激情久久久免费| 久久亚洲AV无码精品色午夜麻豆| 曰批全过程免费视频免费看| 亚洲AV伊人久久青青草原| 成人国产精品免费视频| 亚洲AV无码乱码在线观看牲色| 免费一级毛片在线播放放视频| 亚洲午夜福利精品久久| 在线观看特色大片免费网站| 亚洲精品国产精品国自产观看| 亚洲综合色一区二区三区| 亚洲人成免费网站| 亚洲AV永久青草无码精品| 久久久免费观成人影院| 亚洲精品无码永久在线观看| aaa毛片免费观看| 亚洲最大黄色网址| 免费观看美女裸体网站| 国产成人精品日本亚洲直接| 999久久久免费精品国产| 亚洲欧洲另类春色校园小说| 真实乱视频国产免费观看| 人人爽人人爽人人片av免费| 亚洲国产女人aaa毛片在线 | 免费看美女裸露无档网站| 亚洲码在线中文在线观看|