Glassfish 3.0.1下的Java Web Service不向MySQL数据库插入中文字符

发布于 2024-10-18 01:50:25 字数 1520 浏览 4 评论 0原文

我使用在 Mac OS 上 Glassfish 3.0.1 应用服务器下运行的 NetBeans 6.9.1 创建了一个 Web 服务。它的方法接受一个汉字字符串arg。它将字符串插入 MySQL 数据库表中。在调试模式下单步执行服务器代码时,字符正确到达。调试器观察显示下面的查询参数包含中文字符。

Query query = entityManager.createNativeQuery(QueryInsertWord);
query.setParameter(1, word);
query.executeUpdate();

但是,插入后,数据库表列有 ???而不是汉字。我假设 MySQL 数据库符合 UTF-8 标准,因为我可以使用 Navicat 数据库管理工具成功地将中文字符手动剪切并粘贴到同一列中。

PU 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>javaapplication1.Registration</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myDatabase"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
    </properties>
  </persistence-unit>
</persistence>

我使用附加属性 useUnicode=truecharacterEncoding=utf8 配置了所有 Glassfish jdbc 连接池。

请帮我。

I created a web service using NetBeans 6.9.1 running on the Mac OS under Glassfish 3.0.1 app server. It's method takes a String arg of Chinese characters. It inserts the String into a MySQL database table. When stepping through the server code in debug mode, the characters arrive correctly. A debugger watch shows the query parameter below having the Chinese characters.

Query query = entityManager.createNativeQuery(QueryInsertWord);
query.setParameter(1, word);
query.executeUpdate();

HOWEVER, upon insert, the database table column has ??? instead of the Chinese characters. I assume the MySQL database is UTF-8 compliant because I can successfully manually cut and paste Chinese characters into that very same column using the Navicat database management tool.

The PU looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>javaapplication1.Registration</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myDatabase"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
    </properties>
  </persistence-unit>
</persistence>

I configured ALL Glassfish jdbc connection pools with the additional properties useUnicode=true and characterEncoding=utf8.

Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜`诱少女 2024-10-25 01:50:25

问题至少可能出现在两个地方:

  • 您的连接未使用 utf-8。尝试使用您的网络服务不接受 UTF-8 的连接字符串传递 characterEncoding=utf8
  • (对于此处的操作,您必须提供更多信息)

同时确保您的数据库是 utf_general_ci代码>(或类似代码)

The problem can be in at least two places:

  • your connection is not using utf-8. Try passing characterEncoding=utf8 with the connection string
  • your web service is not accepting UTF-8 (for what to do here you have to provide more information)

Also make sure your database is utf_general_ci (or similar)

木槿暧夏七纪年 2024-10-25 01:50:25

问题解决了。 Web服务的字符编码确实是UTF-8。 MySQL数据库也是UTF-8。问题似乎出在使用 Eclipse JPA 2.0 库的持久性单元中。我怀疑这一点是因为当我使用相关参数作为字节[]而不是字符串组成本机查询时,正确的字符被保留在数据库中。总之,我现在将表达式“data.getBytes(Charset.forName("UTF-8"))”传递给查询参数对象,而不是字符串“data”。由于数据确实采用 UTF-8 格式,因此 getBytes 调用可以正确解释字节数组。重载的参数对象在最终到达MySQL数据库之前将字节数组传递给实体管理器及其持久化库。 MySQL 数据库是 UTF-8,具有 LongText 类型的列。然后将字节正确插入到该列中。如果有人认为我的假设有误,请随时发表进一步评论。我需要继续该项目,因此,在这个特定时间我不会进一步深入研究这个问题。感谢大家的帮助,特别是 Bozho,他让我找到了正确的方向。

The problem is solved. The character encoding of the web service is indeed UTF-8. The MySQL database is also UTF-8. The problem SEEMS to have been in the persistence unit which uses the Eclipse JPA 2.0 library. I suspect this because when I composed the native query with the parameter in question as a byte[] as opposed to a string, the correct characters were persisted in the database. In summary, I now pass the expression "data.getBytes(Charset.forName("UTF-8"))" to the query parameter object, as opposed to the string "data". Since the data is indeed in UTF-8, the getBytes call interprets the byte array correctly. The overloaded parameter object passes the byte array to the entity manager and its persistence library before it finally reaches the MySQL database. The MySQL database is UTF-8 with a column of type LongText. The bytes are then inserted correctly into that column. Please feel free to comment further if anyone feels that I am assuming anything incorrectly. I needed to move on with the project and, therefore, am not delving into the matter further at this particular time. Thank you all for your help, especially Bozho who put me looking in the right direction.

烟沫凡尘 2024-10-25 01:50:25

尝试用这个,
将查询字符串附加到 jdbc url

&characterEncoding=utf8&characterSetResults=utf8

最后应显示如下:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/DATABASE_NAME?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&characterSetResults=utf8"/>

Try with this,
Append the query string to jdbc url

&characterEncoding=utf8&characterSetResults=utf8

At the end should show as this:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/DATABASE_NAME?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&characterSetResults=utf8"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文