Solutions for connecting to mysql database garbled by java

Source: Internet
Author: User

Solution 1:

Encoding during mysql installation,

Check whether my. ini exists.

[Mysql]

Default-character-set = utf8

[Client]
Default-character-set = utf8

[Mysqld]
Default-character-set = utf8:

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; jsp header:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> if not, try:

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.

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

String name = new String (rst. getString ("name"). getBytes ("ISO-8859-1"), "UTF-8"); 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 the web. xml file of your project a or web project:

<Filter>
<Filter-name> UTFEncoding </filter-name>
<Filter-class> com. founder. chenph. Util. EncodingFilter_UTF8

</Filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> UTFEncoding </filter-name>
<Url-pattern> *. jsp </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-name> UTFEncoding </filter-name>
<Url-pattern> *. do </url-pattern>
</Filter-mapping>

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.