Glassfish 3.0.1下的Java Web Service不向MySQL数据库插入中文字符
我使用在 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=true
和 characterEncoding=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题至少可能出现在两个地方:
characterEncoding=utf8
同时确保您的数据库是
utf_general_ci
代码>(或类似代码)The problem can be in at least two places:
characterEncoding=utf8
with the connection stringAlso make sure your database is
utf_general_ci
(or similar)问题解决了。 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.
尝试用这个,
将查询字符串附加到 jdbc url
最后应显示如下:
Try with this,
Append the query string to jdbc url
At the end should show as this: