Hibernate串連MySQL資料庫亂碼相關問題

來源:互聯網
上載者:User

標籤:io   os   ar   使用   sp   檔案   資料   on   問題   

1、查看MySQL字元編碼

  >show variables like ‘character%‘; #執行編碼顯示

  其中character_set_client,character_set_results,character_set_connection三個運行變數是造成亂碼的關鍵

2、修改MySQL編碼格式為UTF8

  編輯my.ini,(注意:不是utf-8,也要注意大小寫)

  找到用戶端配置[client] 在下面添加

  ### 預設字元集為utf8

  default-character-set=utf8

  在找到[mysqld] 添加

  ### 預設字元集為utf8

  default-character-set=utf8

  ### (設定串連mysql資料庫時使用utf8編碼,以讓mysql資料庫為utf8運行)

  init_connect=‘SET NAMES utf8‘

  修改好後,重新啟動mysql 即可,查詢一下show variables like ‘character%‘;

3、同時建立hibernate資料庫時需要顯示設定資料庫的編碼方式為utf8。樣本:

  create database daycode default charset=utf8;

4、做完這兩步還是不行,需要修改hibernate的設定檔hibernate.cfg.xml,在設定檔配置hibernate.connection.url屬性。樣本:

  <property name="hibernate.connection.url">
          <![CDATA[jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8]]>
  </property>

注意:此字串不能寫為jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8,不然會出現編譯錯誤,錯誤提示為將&串連符改為;。

設定這些之後亂碼問題就解決了。

Hibernate串連MySQL資料庫亂碼相關問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.