Java and mysql garbled

Source: Internet
Author: User
Welcome to the Java Community Forum and interact with 2 million technical staff. solution 1: (the most important method) Check out my. ini, with or without a [mysql] default-character-setutf8 [client] default-character-setutf8 [mysqld] default-character-setutf8 and then when creating a table, also use (for example) D

Welcome to the Java Community Forum and interact with 2 million technical staff> solution 1: (the most important method) Check out my. ini, with or without [mysql] default-character-set = utf8 [client] default-character-set = utf8 [mysqld] default-character-set = utf8, also use (for example) D

Welcome to the Java Community Forum and interact with 2 million technical staff> enter

Solution 1: (the most important method)

Check whether my. ini exists.

[Mysql]

Default-character-set = utf8

[Client]

Default-character-set = utf8

[Mysqld]

Default-character-set = utf8

And then use (for example) when creating a table)

Drop table if exists 'company ';

Create table if not exists 'company'

(

'Cname' VARCHAR (10) not null,

'Caddr 'VARCHAR (100 ),

'Ctel' VARCHAR (16)

) ENGINE = InnoDB default charset = UTF8;

In this way, there is no problem.

If jsp is used to declare:

ContentType = "text/html; charset = gb2312"

Solution 2:

When connecting to mysql (whether reading data from mysql or retrieving data), specify the UTF-8 encoding method. The specific code is as follows:

// Load the mysql-jdbc driver

Class. forName ("com. mysql. jdbc. Driver"). newInstance ();

// Connect to the database

Connection sqlCon = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test? User = root & password = 1 & useUnicode = true & characterEncoding = UTF-8 ");

Solution 3:

If method 1 does not work, perform forced encoding conversion on the read String Based on method 1.

The sample code is as follows:

String name = rst. getString ("name ");

Name = new String (name. getBytes ("ISO-8859-1"), "UTF-8 ");

Note: The Code can also be: String name = new String (rst. getString ("name "). getBytes ("ISO-8859-1"), "UTF-8"); where rst is the returned resultset, The ISO-8859-1 is the default encoding method for mysql, the purpose of the code is to convert the ISO-8859-1 encoding to gb2312 encoding mode, this forced conversion, can solve a part of the problem, if combined with method 1, it should be able to solve the Chinese garbled problem.

Solution 4:

This method is the first in some articles. I tried this method first, but it does not seem to work. I am not sure if my operation is incorrect. Post the original text first:

"If the database contains Chinese characters and the default mysql language is not Chinese, add a line in [mysqld] in the configuration file C: winntmy. ini:

Default-character-set = UTF-8

Then restart mysql

Method 5: change it in web. xml in tomcat conf:

  

   UTFEncoding

   Com. founder. chenph. Util. EncodingFilter_UTF8

  

  

   Encoding

   UTF-8

  

  

  

   UTFEncoding

   *. Jsp

  

  

   UTFEncoding

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.