[英]how to write chinese caracters into mysql with hibernate?
im making a web application with javascript. 我用javascript制作一个Web应用程序。 Im using java servlet wich is connected with mysql database with hibernate.
我使用java servlet与hibernate连接mysql数据库。 in Servlet there is a String in chinese, and when i try to write that strin into mysql field it shows up as "?"
在Servlet中有一个中文字符串,当我尝试将该strin写入mysql字段时,它显示为“?” in mysql.
在mysql中。 my whole project in netbeans is set to charset=UTF-8.
我在netbeans中的整个项目设置为charset = UTF-8。 I tried to set collation and charset in database to utf-8, utf8mb4, big5 and none of them works.
我试图在数据库中将collation和charset设置为utf-8,utf8mb4,big5,但它们都不起作用。 i also tried to change my column from varchar to nvarchar but when i click apply (mysql workbench) the column is still "varchar".
我也尝试将我的列从varchar更改为nvarchar但是当我单击apply(mysql workbench)时,该列仍然是“varchar”。
this is my servlet 这是我的servlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
String tabla=request.getParameter("tabla");
//String tabla contains chinese caracters
String user=request.getParameter("user");
Configuration myConf = new Configuration();
myConf.configure("hib/hibernate.cfg.xml");
StandardServiceRegistry service = new StandardServiceRegistryBuilder().
applySettings(myConf.getProperties()).build();
SessionFactory myFactory = myConf.buildSessionFactory(service);
Session conn = myFactory.openSession();
Transaction t = conn.beginTransaction();
List<User>upislista;
upislista=conn.createQuery("SELECT u FROM User u WHERE useUsername='"+user+"'").list();
upislista.get(0).setUseKineski(tabla);
t.commit();
conn.close();
Hibernate XML: Hibernate XML:
<property name="hibernate.connection.CharSet">utf8mb4</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
Connection url: 连接网址:
db.url=jdbc:mysql://localhost:3306/db_nameuseUnicode=true&character_set_server=utf8mb4
The above changes were enough for me to upgrade from utf8 to utf8mb4 charset scheme. 以上更改足以让我从utf8升级到utf8mb4字符集方案。
As a side note I would like to make one clarification that UTF-8 is the character encoding while utf8mb4 is a character set that MySQL supports. 作为旁注,我想澄清一点,UTF-8是字符编码,而utf8mb4是MySQL支持的字符集。 MySQL's utf8mb4 is a superset to MySQL's utf8.
MySQL的utf8mb4是MySQL的utf8的超集。
Spring/Hibernate filter: Spring / Hibernate过滤器:
<form accept-charset="UTF-8">
Spring/Hibernate: 春/休眠:
<property name="url" value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf-8"/> (or maybe it is =yes)
"Spring": “弹簧”:
@RequestMapping(value = "/getRegion2", produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)
不知道你用的哪个版本的mysql, 但是你需要配置mysql 不知道你用的哪个版本的mysql,但是你需要配置mysql
character_set_server=utf8mb4
一些版本hibernate能够正确配置,但是一些版本需要你手动配置。 一些版本的Hibernate能够正确配置,但是一些版本需要你手动配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.