Java connection MySQL database garbled solution

Source: Internet
Author: User

Workaround One:

MySQL installation when the code,

Look at the My.ini, there's no

[MySQL]

Default-character-set=utf8

[Client]
Default-character-set=utf8

[Mysqld]
Default-character-set=utf8 set up when creating a table:

DROP TABLE IF EXISTS ' Company ';
CREATE TABLE IF not EXISTS ' company '
(
' Cname ' VARCHAR not NULL,
' Caddr ' VARCHAR (100),
' Ctel ' VARCHAR (16)
) Engine=innodb DEFAULT Charset=utf8; JSP headers:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" if not, try:

Contenttype= "text/html;charset=gb2312" solution two:

When you connect to MySQL (regardless of whether you are reading or fetching data from MySQL), you specify that the encoding used is utf-8, as follows

Load MYSQL-JDBC Drive

Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();

Connecting to a database

Connection Sqlcon = drivermanager.getconnection ("jdbc:mysql://localhost:3306/test?user=root&password=1& Useunicode=true&characterencoding=utf-8 "); Workaround Three:

If the method fails, then the read string is forced to be converted on the basis of method one.

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"); workaround four:

This method is the first in some articles, I also tried this method, but it seems not. Here is still to say, I do not know whether the operation is wrong. Let's stick to the original:

"Add a row to [mysqld] in profile C:winntmy.ini if you have Chinese in the database and the default language for MySQL is not Chinese":

Default-character-set=utf-8

And then start MySQL again.

Method Five: Change in the Web.xml under TOMCA 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>  
    &nbSp <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.