日期:2014-05-16  浏览次数:20917 次

MySql不能插入中文问题解决
mysql插入中文报错:incorrect string value:...
今天尝试了jsp 中连接mysql数据库插入中文一直报错,搞了我一天的时间

到晚上终于搞定了。

进入安装目录下的 my.ini修改配置,
有两个地方需要修改,一个是client端的还有一个是server端的

Sql代码 
1. # CLIENT SECTION 
2. # ----------------------------------  
3. #  
4. # The following options will be read by MySQL client applications.  
5. # Note that only client applications shipped by MySQL are guaranteed  
6. # to read this section. If you want your own MySQL client program to 
7. # honor these values, you need to specify it as an option during the  
8. # MySQL client library initialization.  
9. #  
10. [client]  
11. port=3306  
12. [mysql]  
13. default-character-set=gbk//这个地方  
14.  
15. # SERVER SECTION 
16. # ----------------------------------  
17. #  
18. # The following options will be read by the MySQL Server. Make sure that  
19. # you have installed the server correctly (see above) so it reads this   
20. # file.  
21. #  
22. [mysqld]  
23. # The TCP/IP Port the MySQL Server will listen on 
24. port=3306  
25.  
26. #Path to installation directory. All paths are usually resolved relative to this.  
27. basedir="C:/Program Files/MySQL/MySQL Server 6.0/" 
28. #Path to the database root  
29. datadir="C:/Program Files/MySQL/MySQL Server 6.0/Data/" 
30. # The default character set that will be used when a new schema or table is 
31. # created and no character set is defined  
32. default-character-set=gbk//这个地方  
33.   


其实这两个地方弄好了,未必就能插入中文,

还要在创建数据库的时候 指定 default charset=gbk

创建表的时候最好也加上。
create table data(
id int primary key auto_increment,
sid varchar(256) not null,
name varchar(256) not null,
        longitude decimal(10,6) not null,
latitude decimal(9,6) not null,
jam_level integer(1) not null,
description text,
insert_time timestamp not null,
update_time timestamp not null

)default charset=gbk

按以上操作后,中文可以插入mysql数据库中了(最好此时重启下mysql服务:net stop mysql,net start mysql)。