JSP编码,而在MySQL数据库中插入非英语文本 [英] JSP encoding while inserting non-English text in MySQL database

查看:94
本文介绍了JSP编码,而在MySQL数据库中插入非英语文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ajax调用在MySQL数据库中插入印度字符。我在应用程序的流程之间面临着一个UTF-8编码问题。



当我通过JDBC直接插入非英语字符时(不使用Ajax调用) ,然后它显示????



当我包含

  response.setCharacterEncoding UTF-8); 
request.setCaracterEncoding(UTF-8);
response.setContentType(text / html; charset = UTF-8);

在我的JSP文件中,然后我收到以下在我的数据库个字符):




b $ b

当我不包括上面的行,它显示我在数据库中的垃圾字符:


àªà?


实际值是



JSP通过JDBC jdbc连接器。



我在所有的JSP文件中添加了以下标记,以确保字符编码:

 <%@ page contentType =text / html%> 
<%@ page pageEncoding =UTF-8%>

 < meta http-equiv =Content-Typecontent =text / html; charset = utf-8; charset = UTF-8> 

我检查MySQL表是否启用了Unicode,我可以通过终端正确地输入非英语文本。 / p>

如何解决这个问题,我该如何解决?



现在,但是当我混合一些查询和插入语句然后...我的应用程序返回我以下错误:
非法混合排序(latin1_swedish_ci,IMPLICIT)和(utf8_general_ci,COERCIBLE)操作'= '
以下是我的数据库变量:

  | Variable_name |价值| 
+ -------------------------- + ------------------ ---------- +
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem |二进制|
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | / usr / share / mysql / charsets / |
| collat​​ion_connection | utf8_general_ci |
| collat​​ion_database | utf8_general_ci |
| collat​​ion_server | utf8_general_ci |
| completion_type | 0 |
| concurrent_insert | 1 |
| connect_timeout | 10 |


解决方案


我通过JDBC直接插入非英语字符(不使用Ajax调用),然后它显示????


这只会发生在双方都完全知道每边的字符编码差异。任何未被另一端使用的字符编码覆盖的字符将由问号替换。否则,您将看到 Mojibake



在这种特殊情况下,这些方面是Java端和数据库端,JDBC驱动程序作为中介。要解决这个问题,你需要告诉JDBC驱动程序这些字符是什么编码。你可以通过设置 useUnicode = true& characterEncoding = UTF-8 JDBC连接URL。

  jdbc:mysql:// localhost:3306 / dbname?useUnicode = true& ; characterEncoding = UTF-8 

然后,根据您从客户端发送参数的方式服务器,您可能还需要修复请求编码。鉴于您在删除 request.setCharacterEncoding(UTF-8)时看到Mojibake,因此您正在使用POST。



对于这种情况,如果您使用GET发送参数,则需要在服务器端配置URI编码。目前还不清楚您使用的是什么服务器,但是例如Tomcat,它是编辑 / conf中的< Connector> /server.xml ,如下所示:

  。URIEncoding =UTF-8> 



另请参阅:




I am using Ajax call to insert Indian characters in MySQL database. I am facing an UTF-8 encoding problem in between flow of my application.

When I am inserting the non-English characters directly by JDBC (not using an Ajax call), then it's showing "????" in the database.

When I include

response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");

in my JSP file, then I receive the following in my database (question marks instead of non-English characters):

????????

When I do not include above lines it shows me junk character like this in database:

મà«?àª?પà«?ષà«?àª

Whereas the actual value is

મખપષ

So the actual problem lies in or after sending insert request to MySQL command in JSP through JDBC jdbc connector.

I have included following tags in all my JSP files to ensure character encoding:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

and

<meta http-equiv="Content-Type" content="text/html; charset=utf-8;charset=UTF-8">

I checked MySQL tables are Unicode enabled and I am able to enter correctly non English text through terminal.

How is this problem caused and how can I solve it?

Now, i am able to write using a insert statement only....but when i mix some queries and insert statement then... my application return me following error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '=' following are my database variables:

| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8_general_ci            |
| collation_database       | utf8_general_ci            |
| collation_server         | utf8_general_ci            |
| completion_type          | 0                          |
| concurrent_insert        | 1                          |
| connect_timeout          | 10                         |

解决方案

When I am inserting the non-English characters directly by JDBC (not using an Ajax call), then it's showing "????" in the database.

This will only happen when the both sides are perfectly aware of the character encoding differences in each side. Any character which is not covered by the character encoding used on the other side will be replaced by a question mark ?. Otherwise you would have seen Mojibake.

In this particular case, those sides are the Java side and the database side, with the JDBC driver as mediator. To fix this, you need to tell the JDBC driver what encoding those characters are in. You can do that by setting the useUnicode=true&characterEncoding=UTF-8 parameters in the JDBC connection URL.

jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8

Then, depending on how you're sending the parameters from the client to server, you possibly also need to fix the request encoding. Given the fact that you're seeing Mojibake when you removes request.setCharacterEncoding("UTF-8"), you're using POST. So that part is fine.

For the case that, if you were using GET to send the parameters, you would need to configure URI encoding in the server side. It's unclear what server you're using, but in case of for example Tomcat, it's a matter of editing the <Connector> entry in /conf/server.xml as follows:

<Connector ... URIEncoding="UTF-8">

See also:

这篇关于JSP编码,而在MySQL数据库中插入非英语文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆