JSP connection to MySQL garbled solution (all)

Source: Internet
Author: User

Using the MySQL database and Using JSP to connect to the results found a lot of problems, mainly in garbled display above, the actual

During development, such problems may seem strange. After searching many posts on the internet, I tried to repeat them and wrote them down with some tips.

When you encounter a similar problem, you can also prevent yourself from forgetting to come over again:

Http://blog.csdn.net/fly29/archive/2004/07/31/57084.aspx
When I first learned JSP, I encountered a Chinese Garbled text problem. I read many posts on csdn, which is a little enlightening. Fortunately, the problem is solved.

Today, we encountered a problem where garbled characters were displayed in Chinese when we passed the page values.

------------------------------

Name:

Email:

 

 

<% @ Page Language = "Java" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>

---------------------------------

Information Transmission between servers uses iso8859_1
The browser uses gb2312 for display.
When getparameter () is used, the information transmitted between servers is obtained.
Therefore, specify the encoding. This is getbytes ("iso8859_1 ")
It is then used for display, so it must be converted to gb2312
So string S = new string (getparameter ("XXXX"). getbytes ("iso8859_1"), "gb2312 ");
----------------------------------

I have learned many things and found many questions about Chinese garbled characters on the internet, involving database operations. Hi, just because you

Too many dishes. But you need to post it first. You can use it later.

------------------------------------
Change the connection string:
String url = "JDBC: mysql: // server/tzw? Useunicode = true & characterencoding = gb2312 ";

This method,
-------------------------------------

The reason for Chinese garbled characters may also involve OS, JDBC version, DBMS...

Learning...

I have searched many articles in the previous article. He understands the main principles of Garbled text, as mentioned above.

The problem of pass-through value.

The following is a good summary:
Summary of JSP Chinese garbled problem Solutions
Http://blog.csdn.net/chm_y/archive/2004/07/03/33277.aspx
In the process of using JSP, Chinese garbled characters are the biggest headache. The following are the garbled characters I encountered in software development.

And solutions.

1. garbled JSP pages
The reason for this Garbled text is that the character set encoding is not specified on the page. Solution: Use the following code at the beginning of the page.

The Code specifies the character set encoding:

<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "Err. jsp" %>

2. Database garbled characters
This Garbled text will make Chinese characters you inserted into the database Garbled text or Garbled text when reading the display. The solution is as follows:
Add the encoding character set to the database connection string
String url = "JDBC: mysql: // localhost/digitgulf? User = root & Password = root & useunicode = true & characterencoding = gb2312 ";
Use the following code on the page:
Response. setcontenttype ("text/html; charset = gb2312 ");
Request. setcharacterencoding ("gb2312 ");

3. garbled characters are transmitted as parameters in Chinese.
When we pass a Chinese character as a parameter to another page, garbled characters also occur. The solution is as follows:
Encode parameters when passing parameters, such
Rearshres. jsp? KEYWORDS = "+ java.net. urlencoder. encode (keywords)
Then, use the following statement on the receiving parameters page to receive
KEYWORDS = new string (request. getparameter ("keywords"). getbytes ("8859_1 "));

The above is the garbled problem encountered at the present stage. The core issue of garbled code is the character set encoding problem. As long as you master this problem, it is generally messy.

Code problems can be solved.

 

Overall:
There are two reasons for garbled code connecting JSP to MySQL: one is the reason for setting JSP pages and the other is the original character setting of MySQL database.

Because, we should solve the latter first.
First, go to MySQL and run the command: Show create table test (test is the name of a table in your specific database). However

The following information appears:
Create Table 't_ course '(
'Id' int (11) not null,
'Course _ Code' varchar (10) Not null,

'Course _ name' varchar (50) default null,
'College 'varchar (10) default null,

'Course _ time' varchar (10) default null,
'Start _ end' varchar (10) default null,

'Course _ tech 'varchar (10) default null,
'Course _ credentials' varchar (10) default null,

'Course _ site' varchar (30) default null,
'Course _ note' text
) Engine = InnoDB default

Charset = gb2312

The important part of this information is the following: "Default charset = gb2312". If this information is not used, it is "default ".

Charset = Latin1 ", the insert into test values ('China') will

In case of garbled characters, it is also garbled to insert Chinese data to MySQL through JSP execution, so I will change it to the charset shown above.

(Default charset = gb2312 ).
There are several methods:
1. Run the set names GBK statement before creating the table;
For example:
Set names GBK;

Drop table if exists grade;
Create Table grade (
Name varchar (30) not null default '',
Note char (3) not null default '',
Id tinyint (4) not null default '0 ',
Primary Key (ID)
) Type = MyISAM;

 

--
-- Dumping data for table 'grad'
--

Insert into grade values ('1st 1', 'c1', 2 );
Insert into grade values (' ', 'c2', 3 );
Insert into grade values (' ', 'c3', 4 );
Insert into grade values ('Junior high 1', 'C', 1 );
Insert into grade values ('1100', 'g1', 6 );
Insert into grade values ('sophomore year, 'g2', 7 );
Insert into grade values ('high school', 'G', 5 );
Insert into grade values ('Senior science ', 'g3l', 9 );
Insert into grade values ('high school liberal arts ', 'g3w', 10 );

In this way, the data can be inserted normally.

2. For the data insertion prompt: data too long for column 'name' at Row 1
One is the configuration file my. ini.
One is to modify the SQL statement and add GBK
If none of the above methods works, replace the MySQL driver with the latest version.

3. If you enter a database with Some uncommon words, the following error occurs: data too long for column 'name' at Row 1.
You can add: Set names GBK before the input data.

4. Why is an error prompted when I insert a record to the MySQL database (Version 5.0) when the content of a field is in Chinese?

The solution is as follows:

Create Table rz_user (
Id integer not null auto_increment,
Name varchar (32 ),
Password varchar (32 ),
Primary Key (ID)
);

-- Insert statement:

Set names GBK;

Insert into rz_user values (1, 'friends', '123 ');

Solution: Add: Set names GBK before inserting data;

5,
MySQL Chinese garbled characters in JSP.
I tried to use MySQL as the database in JSP the day before yesterday, but I encountered a garbled problem as soon as I got started. I put the charset in the page

The class is changed to gb2312, and no garbled code is displayed on the page. Later, I checked the information and solved the problem easily. When I set up a database

Use create database zzz default character setgb2312; to solve the problem.

6. Solve Chinese garbled characters in JSP reading data from MySQL
All the places are set to UTF-8 encoding, according to the principle should be no problem, but the Chinese write database is no problem, the database

Chinese characters are displayed, but garbled characters are displayed. In mysql5.0, when I used the text type, Chinese characters were garbled during reading. Solution

The solution is new string (Rs. getstring (I). getbytes ("ISO-8859-1"), "UTF-8 "). Put the original Rs. getstring (I)

Encode and convert.

7. character_set_database: Show variables is always Latin1, and set character_set_database = utf8 is also used.

It cannot be changed!

My. ini may be included, or it is specified during compilation.
In my. CNF or my. ini, add
Default-character-set = utf8
After saving, restart mysql. If it cannot be started, you can only reinstall it. During installation, specify the character set.

8. Default character set for MySQL installation:
| Character_set_client | Latin1 |
| Character_set_connection | Latin1 |
| Character_set_database | Latin1 |
| Character_set_results | Latin1 |
| Character_set_server | Latin1 |
| Character_set_system | utf8 |

 

9. MySQL Chinese display problems
Http://cache.baidu.com/C? WORD = % D0 % de % B8 % c4 % 2 ccharacter % 3B % 5f % 3 bset % 3B % 5f %

3 bdatabase & url = http % 3A // www % 2 ezhanso % 2 ECOM/Java % 5F1% 5f17308%

2 ehtml & P = c062c64ad58757fe08e2912150 & User = Baidu
Q:
Save Chinese characters from the JavaBean to the database and display it "? "However, when I save Chinese characters to the database on the JSP page, it will be displayed normally.

Show variables like % char %; character_set_client: GBK character_set_connection: GBK

Character_set_database: Latin1 character_set_results: GBK character_set_server: utf8

How to solve Shira
I still cannot convert the code using this method to Public String tochinese (string Str) {If (STR = NULL |

Str. Length () <1) {STR = "";} else {try {STR = (new string (Str. getbytes ("ISO-8859-1 "),

"Gb2312");} catch (unsupportedencodingexception e) {system. Err. Print (E. getmessage ());

E. printstacktrace (); Return STR ;}} return STR ;}
Reply to Chinese 3mysql display questions
My solution is that all JSP pages are UTF-8, and then convert the value obtained from the page to utf8, the corresponding field of the database is also UTF-8 can

To view my blog http://syhan.javaeye.com/blog/59113
Reply to 4mysql Chinese display questions
If this method is used, it will change too much. Is there any simple method?
Reply to question about 5mysql Chinese display
All the codes are unified. If you use one, you don't need to convert it any more, or worry about garbled code. We recommend that you use GBK.
Reply to the 6mysql Chinese display question
Character_set_database: Latin1 this is changed to UTF-8 or gb2312 try
Reply to the 7mysql Chinese display question
How to change character_set_database: Latin1?
Reply to the 8mysql Chinese display question
Mysql_front can change character-related to gb2312
Reply to 9mysql Chinese display questions
If it is win, modify my. ini else and modify my. CNF under [mysqld ].

 

10: Solve the Chinese problem of MySQL and JSP in Linux
Http://syhan.javaeye.com/blog/59113
Keyword: MySQL Chinese garbled JSP
My environment is Ubuntu 6.10, MySQL 5.0.24a-debian_9ubuntu1 (directly apt-Get), Tomcat 5.5.20

This is also a classic problem, which can be easily viewed after being written here.

There is no originality. In combination with the experience of our predecessors, my steps are as follows:

 

1. Modify/etc/MySQL/My. CnF in

Add a row under [mysqld]

Java code
[Mysqld]
Default-character-set = utf8

After saving and exiting, restart the MySQL Service

 

Java code
Sudo/etc/init. d/MySQL restart

2. Set pageencoding for all JSP pages that need to enter Chinese as a UTF-8, that is, add

Java code
<% @ Page Language = "Java" pageencoding = "UTF-8" %>

3. The Chinese strings received from the page are transcoded before they are added to the database, which is also very simple.

Java code
 
Public static string getdecodedstring (string Str)

{

Try {

Return new string (Str. getbytes ("ISO8859-1"), "UTF-8 ");

} Catch (unsupportedencodingexception e ){

E. printstacktrace ();

}

Return STR;

}

This is because Tomcat's default encoding format is ISO8859-1, of course, you can also re-compile the Tomcat source code to the default encoding

Code to UTF-8, this is another way, I have not tried

In addition, fliter is used to change the encoding format of all input data on the page.

Write a conversion class (similar to the preceding one), and configure it in Web. xml.

4. When the database is created, set charset of fields containing Chinese characters to UTF-8, such

SQL code
 
Create Table Test (

'Name' varchar (100) Character Set utf8 collate utf8_unicode_ci not null default''

)

In this way, MySQL supports Chinese characters.

 

 

Digress: mysql-administrator has a disgusting bug, that is, the entire interface is displayed during user administration.

Dead, console reports

CPP Code
 
** (MySQL-Admin: 20978): Critical **: void mgfilebrowserlist: get_row_object (const

GTK: treeiter &, STD: string &): assertion 'iter 'failed

 

Fixed release already exists on the Internet, but unfortunately at least on my Ubuntu

 

11. Summary of JSP Chinese garbled problem Solutions
Http://hi.baidu.com/liuzy84/blog/item/1a6eebfa9e4f3fdeb48f31f3.html
In the process of using JSP, Chinese garbled characters are the biggest headache. The following are the garbled characters I encountered in software development.

And solutions.
1. garbled JSP pages

The reason for this Garbled text is that the character set encoding is not specified on the page. Solution: Use the following code at the beginning of the page.

The Code specifies the character set encoding,

2. Database garbled characters

This Garbled text will make Chinese characters you inserted into the database Garbled text or Garbled text when reading the display. The solution is as follows:
Add the encoding character set to the database connection string
String url = "JDBC: mysql: // localhost/digitgulf?

User = root & Password = root & useunicode = true & characterencoding = gb2312 ";
Use the following code on the page:
Response. setcontenttype ("text/html; charset = gb2312 ");
Request. setcharacterencoding ("gb2312 ");

3. garbled characters are transmitted as parameters in Chinese.

When we pass a Chinese character as a parameter to another page, garbled characters also occur. The solution is as follows:
Encode parameters when passing parameters, such
Rearshres. jsp? KEYWORDS = "+ java.net. urlencoder. encode (keywords)
Then, use the following statement on the receiving parameters page to receive
KEYWORDS = new string (request. getparameter ("keywords"). getbytes ("8859_1 "));

4. Add this sentence to JSP page garbled characters?

<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL .*"

Errorpage = "Err. jsp" %>

 

12. When doing JSP jobs, the JSP connection to MySQL is garbled.
Http://blog.csdn.net/VooStone/archive/2006/06/16/803928.aspx
Asked, he said that as long as the next mysql-font can solve the problem, at that time it did solve the problem of garbled code.

But the second time MySQL was started, there was a garbled problem. After Google,
We found that we only need to change the character-set in my. ini to gb2312, including the server and client.

The problem is solved ..

 

13. Java Chinese garbled Solutions
Http://blog.csdn.net/bineon/archive/2004/08/09/69652.aspx
1. Output Chinese characters on the webpage.
Java is used in network transmission encoding is "ISO-8859-1", so in the output need to be converted, such:
String STR = "Chinese ";
STR = new string (Str. getbytes ("gb2312"), "8859_1 ");
However, if the encoding used during program compilation is "gb2312" and the program runs on the Chinese platform, this problem does not occur.

Note.
2. Read Chinese from Parameters
This is exactly the opposite of the output in the webpage:
STR = new string (Str. getbytes ("8859_1"), "gb2312 ");
3. Questions about how to operate databases in Chinese
A simple method is to set "region" to "English (USA)" in "control plane )". If garbled characters still appear, you can

Perform the following settings:
When retrieving Chinese characters: Str = new string (Str. getbytes ("gb2312 "));
Input Chinese characters to DB: Str = new string (Str. getbytes ("ISO-8859-1 "));

4. Solutions for Chinese in JSP:

In "control plane", set "region" to "English (USA )".
Add:
If not, perform the following conversion:
For example: Name = new string (name. getbytes ("ISO-8859-1"), "GBK ");
There will be no Chinese problems.

 

14. garbled characters are displayed in Chinese! (Add ...)
Http://blog.csdn.net/fly29/archive/2004/07/31/57084.aspx

When I first learned JSP, I encountered a Chinese Garbled text problem. I read many posts on csdn, which is a little enlightening. Fortunately, the problem is solved.

Today, we encountered a problem where garbled characters were displayed in Chinese when we passed the page values.

------------------------------

Name:

Email:

 

 

<% @ Page Language = "Java" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>

---------------------------------

Information Transmission between servers uses iso8859_1
The browser uses gb2312 for display.
When getparameter () is used, the information transmitted between servers is obtained.
Therefore, specify the encoding. This is getbytes ("iso8859_1 ")
It is then used for display, so it must be converted to gb2312
So string S = new string (getparameter ("XXXX"). getbytes ("iso8859_1"), "gb2312 ");
----------------------------------

I have learned many things and found many questions about Chinese garbled characters on the internet, involving database operations. Hi, just because you

Too many dishes. But you need to post it first. You can use it later.

------------------------------------
Change the connection string:
String url = "JDBC: mysql: // server/tzw? Useunicode = true & characterencoding = gb2312 ";

This method,
-------------------------------------

The reason for Chinese garbled characters may also involve OS, JDBC version, DBMS...

Learning...

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 57084

15. Question about MySQL operations in JSP ???
Http://topic.csdn.net/t/20030505/17/1742215.html
Why do I add Chinese data to the MySQL database in JSP? Chinese data displayed in MySQL is garbled and read in the JSP program,

It's also garbled. Is it the same with internal code conversion? How can this problem be solved?

Number of questions: 0, number of replies: 7
Top
 

1 floor dcmj (second level) reply to 17:54:38 score 0 try:

1.
Generally, receives the passed Chinese String
String STR = new string (request. getparameter ("str"). getbytes ("8859_1 "));
2.
Import = "java.net. urlencoder"
Import = "java.net. urldecoder"
.......
Transmission Time
Urlencoder. encode (_ Str );
Connection time
String STR = urldecoder. Decode (_ Str );

You must receive the following messages in Tomcat:
String STR = urldecoder. Decode (new string (request. getparameter ("_ Str"). getbytes

("8859_1 ")));
Top

On the second floor, ienet (sys) replied to the first method with a score of 0 at 20:58:02.
The second method has not been tried yet.
Top

On the third floor, baosn () replied to 21:23:06 score 0. display Chinese in JSP webpage as long as the following words are added to JSP webpage

Sentence:
<% @ Page contenttype = "text/html; charset =" gb2312 "%>
Before inserting Chinese characters into the database, use the following function for conversion:
Public static string tochinese (string strvalue ){
Try {
If (strvalue = NULL)
Return NULL;
Else {
Strvalue = new string (strvalue. getbytes

("Iso8859_1"), "GBK ");
Return strvalue;
}
} Catch (exception e ){
Return NULL;
}
}
Top

On the 4th floor, snicker (my smile) replied to the following message: 08:52:17 score 0 JSP page added:
<% @ Page contenttype = "text/html; charset =" gb2312 "%>

Add
"? Useunicode = true & characterencoding = gb2312"


Top

On the fifth floor, zheng_wei99 (less detours) replied to the problem that I scored 0 at 09:08:15. Sigh, pain

!
Top

On the 6th floor, when zhangsq (medium zhangsq) returns to the database connection score of 09:22:56 0:
Tring connstr = "JDBC: mysql: // localhost: 3306/flystar?

Useunicode = true & characterencoding = gb2312 ";
You can! MySQL is used.
Top

Jeoky (jeoky) on the 7th floor replied to 19:20:01,-03-13, with a score of 0. 1. Do not reference the data insertion Operation page. <% @

Page contenttype = "text/html; charset =" gb2312 "%>, the Chinese data displayed in MySQL is correct;
2. When data is displayed on the page, <% @ page contenttype = "text/html; charset =" gb2312 "%> is used;
3. When querying fields with Chinese characters, encode and convert keywords
Example: name = "program ";
Name = new string (name. getbytes ("iso8859_1"), "gb2312 ");
In this way, the query can be correctly performed;

These summaries help you understand and solve MySQL Chinese garbled characters.

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.