mysql Chinese garbled solution

hibernate and mysql Chinese garbled problem

(1) View the default encoding format

show variables like "%char%";

(2) Set the encoding format as shown in the figure below

show variables like "%char%";
SET character_set_database='utf8';
SET character_set_server='utf8';
show variables like "%char%";

(3) Check the database encoding format, find it is Latin format, change it to utf-8

show create table online_chat;

ALTER DATABASE `online_chart` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

(4) View and set the default encoding format of the table

user online_chart;
show create table userbean;

Found it is in Latin format, change it to utf8

ALTER TABLE `userbean` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `educationrecord` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

(5) View the encoding format of column attributes

Right-click the table, select alter tables (change table), you can change the encoding format of the column.

(6) If you use hibernate configuration, the configuration source encoding format is changed like this

<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/crawler_paper?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

Guess you like

Origin blog.csdn.net/khuangliang/article/details/53366119