default-character-set=utf8
create
database
mydb
default
character
set
utf8
collate
utf8_general_ci
3、建立数据表示也要指定字符集:
解决方法:1、打开mysql安装目录下的my.ini文件,找到如下代码:
# CLIENT SECTION
# ----------------------------------------------------------------------
[client]
port=3306
[mysql]
default-character-set=latin1
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="D:/Program Files/MySQL Server 5.0/"
#Path to the database root
datadir="D:/Program Files/MySQL Server 5.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=latin1
将default-character-set=latin1修改为:default-character-set=gb2312
2、打开hibernate配置文件,添加如下代码:
<property name="url">
jdbc:mysql://localhost/test?user=rootpassword=123456useUnicode=truecharacterEncoding=gbk
</property>
3、重启mysql,ok!
查找了N多资料,自己总结尝试可以,我的就是怎么解决的,如果你的问题解决了,麻烦说一声哦,祝你好运!
mysql中文显示乱码或者问号是因为选用的编码不对或者编码不一致造成的,最简单的方法就是修改mysql的配置文件my.cnf。在[mydqld]和[client]段加入default-character-set=utf8
(有的版本不支持default-character-set=utf8,用character_set_server=utf8来取代 default-character-set=utf8即可)
注:如果没有[client]就手工加入[client]段
( 5.0以上版本修改方法:
修改/etc/my.cnf 中的设置,
在[client]节点下添加
default-character-set=utf8
在[mysqld]节点下添加
character-set-server=utf8
collation-server=utf8_general_ci
)
然后重启mysql即可
附关闭启动命令:(
[root@sg211 mysql-cluster]# bin/mysqladmin -u root -p shutdown
[root@sg211 mysql-cluster]# bin/mysqld_safe --defaults-file=/opt/mysql-cluster/etc/my.cnf --basedir=/opt/mysql-cluster --datadir=/opt/mysql-cluster/data --user=mysql &
)
这时可以验证下是否生效
mysql>show variables like 'char%'
+--------------------------+------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /opt/mysql-cluster/share/mysql/charsets/ |
+--------------------------+------------------------------------------+
8 rows in set (0.00 sec)
可以 看出都已经更正为utf8了,这样新建立的数据库缺省就是UTF8编码了。
那么已经创建好的数据库及库中的表要如何更改为utf8呢?
用alter语句(修改数据库的字符集不会改变原有数据表的字符集)
utf8:
ALTER DATABASE `数据库` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER TABLE `数据表` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
gbk (包含gb2312):
ALTER DATABASE `数据库` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci
ALTER TABLE `数据表` DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)