A few days ago, desperately coding encountered a lot of problems, one of which is the Chinese character Program ape problem-Chinese code garbled.
When I test curd the well-written DAO module, the database executes normally, but all the Chinese in the field are garbled. I tried to modify the Eclipse project by default encoding to UTF-8, trying to set the table encoding for the database
ALTER TABLE type character set UTF8;
is useless.
I have encountered this situation, but did not tidy up, to the use of time, wrinkly, meter is not up.
Tossing for half an hour, finally found the solution:
Forget about the embarrassment here, cough, join us just started building a database table:
We should do this:
CREATE TABLE Jingdian (id int PRIMARY KEY auto_increment, Businessid int, BusinessName varchar ($), City VARCHAR, Lo Ngitude double, latitude double, hashcode VARCHAR (+)) Default CharSet = UTF8;
is to include the definition of the default character set at the end of the creation table: But it's a bit confusing why the table character set operation didn't succeed before it was modified. I guess the reason is that before the database encoding defaults to another type, when I deposit utf-8 encoded data when it is considered to be the default MySQL encoding data and is incorrectly decoded, garbled, and I modified the table character encoding, At this time, MySQL will be the same as the original character of the chaotic code conversion method to the MySQL default code to UTF-8 conversion, wrong add error. The specific reason is still to be examined, here is my speculation.
Then the access URL of the database also needs to specify the parameters, this is I usually do not pay attention to the way, do not need to involve the use of specific database interaction components. With considerable versatility, this is recommended for everyone:
driver=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/database?useunicode=true&characterencoding= UTF8 username=rootpassword=
I looked at other people's blogs and there seemed to be other solutions, and here I looked at a few more clear:
1. SET NAMES ' UTF8 ';
It is equivalent to the following three-sentence instruction:
SET character_set_client = UTF8; SET character_set_results = UTF8; SET character_set_connection = UTF8;
2. Create a database
Mysql> CREATE database name character set UTF8;
3. Create a table
CREATE TABLE ' type ' (' id ' int (ten) unsigned not NULL auto_increment, ' flag_deleted ' enum (' Y ', ' n ') character set UTF8 not N ULL default ' N ', ' flag_type ' int (5) NOT null default ' 0 ', ' type_name ' varchar (#) Character set UTF8 NOT null default ' ', PRIMARY KEY (' id ')) DEFAULT Charset=utf8;
4. Modify the database into UTF8.
mysql> ALTER DATABASE name character set UTF8;
5. Modify the table by default with UTF8.
mysql> ALTER TABLE type character set UTF8;
6. Modify the field with UTF8
Mysql> ALTER TABLE type modify type_name varchar () CHARACTER SET UTF8;
The problem of Chinese coding in database crud