The problem of Chinese coding in database crud

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.