Java connection to mysql database garbled

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

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; 

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 (); // 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> 

Related Article

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.