写入数据库时​​出现错误的UTF-8编码(读取正常) [英] Bad UTF-8 encoding when writing to database (reading is OK)

查看:240
本文介绍了写入数据库时​​出现错误的UTF-8编码(读取正常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 JSF EclipseLink JPA MySQL 数据库的网络应用程序中遇到很大问题。

I have big problem in my web application using JSF and EclipseLink JPA to MySQL database.

当我从数据库读取数据时JSF在UTF-8中读写我的字符。但在数据库字符是坏的。

When I read data from database JSF reads and writes my charachters in UTF-8 OK. but in database characters are bad.

f.e:输入字符:żźćółzxcv,写入数据库:Zxcv。
但是如果我手动将数据写入数据库,例如:żźćółzxcv,那么在JSF中阅读是完美的。

f.e.: input characters: "żźćółzxcv", written in database: "?????zxcv". But if I manually write data to database, for example: "żźćółzxcv", then reading in JSF is perfect.

href =http://stackoverflow.com/questions/9634230/typing-chinese-with-primefaces-peditor-component/9839362#9839362>使用PrimeFaces'< p:editor>键入中文组件

I tried everything from here: Typing Chinese with PrimeFaces' <p:editor> component

我发现JSF中的编码很好,但问题是在java中,如果我手动设置

And I discovered that encoding in JSF is fine, but the problem is in java, becouse if I set manually

current.setUwagiZ("żźćóżźćłąśóżźćł TE");
getFacade().edit(current);

在数据库记录中错误: ??? ?ó???? TE

in database record is wrong: ???ó??????ó???? TE

我在JDBC资源中设置了characterEncoding和useUnicode。此外,当NetBeans编码中的某些工具执行命令并且MySQL中的数据是UTF-8时,所以连接似乎很好。

I have set characterEncoding and useUnicode in JDBC Resource. Also when execute commands by some tools in NetBeans encoding is OK and data in MySQL are in UTF-8, so connections seems fine.

所以问题是java,完全不知道如何解决这个问题:(

So the problem is java, but I completely don't know how to solve this :(

推荐答案

当信使本身知道字符编码与 Mojibake 的区别在于它不是信使的错,而是制作人和/

Question marks can occur when the messenger itself is aware about the character encoding used in the both sides of the transport. That's the difference with Mojibake whereby it's not the messenger's fault, but the producer's and/or consumer's fault.

在具有数据库后端的平均Web应用程序中,只有两个地方可能发生这种情况:与数据库通信并与HTTP客户端通信。您已经排除了HTTP部分,因此留在DB部分之后。

In an average web application with a database backend, there are only 2 places where this can happen: communication with the DB and communication with the HTTP client. You've already excluded the HTTP part, so left behind the DB part.

DB部分中的信使是JDBC驱动程序,您需要告诉JDBC驱动程序使用UTF-8。MySQL JDBC驱动程序默认使用默认的客户端平台默认编码,这在你的特定情况下显然不是UTF-8。

The messenger in the DB part is the JDBC driver. You need to tell the JDBC driver to use UTF-8. MySQL JDBC driver is known to use by default the client platform default encoding, which is in your particular case apparently not UTF-8.

添加以下2个属性到JDBC连接:

Add the following 2 properties to the JDBC connection:


  • useUnicode = true

  • characterEncoding = UTF-8

  • useUnicode=true
  • characterEncoding=UTF-8

你已经配置了JDBC连接,但是如果它是plain vanillaJDBC,则在JDBC URL中将它们指定为查询字符串:

It's unclear how you've configured the JDBC connection, but if it's "plain vanilla" JDBC, then specify them as query string in JDBC URL:

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

$ b b

或者如果它是一个特定于容器的数据源配置,那么将它们指定为单独的连接属性,与指定用户名和密码完全相同。

Or if it's a container-specific datasource config, then specify them as separate connection properties, exactly the same way as you specify the username and password.

  • Unicode - How to get the characters right?

这篇关于写入数据库时​​出现错误的UTF-8编码(读取正常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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