[英]how to write chinese caracters into mysql with hibernate?
我用javascript制作一個Web應用程序。 我使用java servlet與hibernate連接mysql數據庫。 在Servlet中有一個中文字符串,當我嘗試將該strin寫入mysql字段時,它顯示為“?” 在mysql中。 我在netbeans中的整個項目設置為charset = UTF-8。 我試圖在數據庫中將collation和charset設置為utf-8,utf8mb4,big5,但它們都不起作用。 我也嘗試將我的列從varchar更改為nvarchar但是當我單擊apply(mysql workbench)時,該列仍然是“varchar”。
這是我的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:
<property name="hibernate.connection.CharSet">utf8mb4</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
連接網址:
db.url=jdbc:mysql://localhost:3306/db_nameuseUnicode=true&character_set_server=utf8mb4
以上更改足以讓我從utf8升級到utf8mb4字符集方案。
作為旁注,我想澄清一點,UTF-8是字符編碼,而utf8mb4是MySQL支持的字符集。 MySQL的utf8mb4是MySQL的utf8的超集。
Spring / Hibernate過濾器:
<form accept-charset="UTF-8">
春/休眠:
<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)
“彈簧”:
@RequestMapping(value = "/getRegion2", produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)
不知道你用的哪個版本的mysql,但是你需要配置mysql
character_set_server=utf8mb4
一些版本的Hibernate能夠正確配置,但是一些版本需要你手動配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.