【发布时间】:2025-12-20 02:20:10
【问题描述】:
我将 Hibernate 用于 JEE 解决方案。我需要数据库连接为 UTF-8。这是我尝试过的:
数据库端
mysql> select c.character_set_name from information_schema.tables as t, information_schema.collation_character_set_applicability as c where c.collation_name = t.table_collation and t.table_schema = "ir2016" and t.table_name = "personne";
+--------------------+
| character_set_name |
+--------------------+
| utf8 |
+--------------------+
1 row in set (0.01 sec)
我还插入了来自 MySQL Workbench 的示例数据。结果很好地以 UTF-8 编码。所以问题肯定出在JEE服务器端。
JEE 方面
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--Databaseconnectionsettings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/ir2016?useUnicode=true&characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">xxxx</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">utf8</property>
<property name="connection.CharSet">utf8</property>
<!--JDBC connectionpool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!--SQL dialect-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--EnableHibernate'sautomaticsession contextmanagement -->
<property name="current_session_context_class">thread</property>
<!--Disablethe second-levelcache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--Echo all executedSQL to stdout-->
<property name="show_sql">true</property>
<mapping resource="Quiz.hbm.xml"/>
<mapping resource="Proposition.hbm.xml"/>
<mapping resource="Question.hbm.xml"/>
<mapping resource="Personne.hbm.xml"/>
<mapping resource="Choisir.hbm.xml"/>
</session-factory>
</hibernate-configuration>
server.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
JSP 页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
有人可以帮忙吗?
【问题讨论】:
-
那么,有什么问题吗?你不能用 utf-8 写到你的数据库吗?
-
是的,问题是我仍然无法在 utf8 中写入 db
-
第一个输出可能无关紧要;而是提供
SHOW CREATE TABLE personne; -
“不能写”是什么意思??服务器崩溃?什么都没发生?垃圾是放到桌子上的吗?例外?还有什么?
标签: java mysql hibernate utf-8