從記憶體中寫入mysql中文資料亂碼解決

來源:互聯網
上載者:User

標籤:sts   控制   java   drop   setting   null   ref   utf8編碼   mysql   

一. 問題

資料庫編碼:utf8

mysql> create database dbnameDEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

表編碼:utf8

drop table if exists `test`;create table `test` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘ID‘,`name` varchar(50) default ‘‘,`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘建立時間‘,PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;

jdbc url:

url: jdbc:mysql://host:port/dbname

資料庫和資料庫表都已經使用了utf8編碼,但是插入中文資料時仍然亂碼。

 

二. 原因

在jdbc中串連mysql時,jdbc url參數中有一個屬性characterEncoding控制字元串編碼,該值預設為:autodetect。需要明確設定為utf8,可解決問題。

MySQL文檔解釋如下,詳見:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html中"Setting Configuration Properties"部分詳細說明。

 

三. 解決辦法

在jdbc url中明確設定characterEncoding屬性為utf8。

url: jdbc:mysql://host:port/dbname?characterEncoding=utf8

 

從記憶體中寫入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.