how to write chinese caracters into mysql with hibernate?
up vote
3
down vote
favorite
im making a web application with javascript. Im using java servlet wich is connected with mysql database with hibernate. in Servlet there is a String in chinese, and when i try to write that strin into mysql field it shows up as "?" in mysql. my whole project in netbeans is set to charset=UTF-8. I tried to set collation and charset in database to utf-8, utf8mb4, big5 and none of them works. i also tried to change my column from varchar to nvarchar but when i click apply (mysql workbench) the column is still "varchar".
this is my 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();
java mysql database hibernate encoding
|
show 6 more comments
up vote
3
down vote
favorite
im making a web application with javascript. Im using java servlet wich is connected with mysql database with hibernate. in Servlet there is a String in chinese, and when i try to write that strin into mysql field it shows up as "?" in mysql. my whole project in netbeans is set to charset=UTF-8. I tried to set collation and charset in database to utf-8, utf8mb4, big5 and none of them works. i also tried to change my column from varchar to nvarchar but when i click apply (mysql workbench) the column is still "varchar".
this is my 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();
java mysql database hibernate encoding
netbeans and browser can recognize chinese characters, only mysql show them as "???".
– Danilo99
Nov 9 at 15:17
Have you tried changing the font to a more utf8 friendly font? stackoverflow.com/questions/9553386/… en.wikipedia.org/wiki/Unicode_font
– Sedrick
Nov 9 at 15:31
I would tryGNU_Unifontin theResultset Grid:.
– Sedrick
Nov 9 at 15:38
i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLEkineski.kineskiCHANGE COLUMNkin_tekstkin_tekstNVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL
– Danilo99
Nov 9 at 15:46
Did you restart mysql workbench after the change?
– Sedrick
Nov 9 at 15:48
|
show 6 more comments
up vote
3
down vote
favorite
up vote
3
down vote
favorite
im making a web application with javascript. Im using java servlet wich is connected with mysql database with hibernate. in Servlet there is a String in chinese, and when i try to write that strin into mysql field it shows up as "?" in mysql. my whole project in netbeans is set to charset=UTF-8. I tried to set collation and charset in database to utf-8, utf8mb4, big5 and none of them works. i also tried to change my column from varchar to nvarchar but when i click apply (mysql workbench) the column is still "varchar".
this is my 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();
java mysql database hibernate encoding
im making a web application with javascript. Im using java servlet wich is connected with mysql database with hibernate. in Servlet there is a String in chinese, and when i try to write that strin into mysql field it shows up as "?" in mysql. my whole project in netbeans is set to charset=UTF-8. I tried to set collation and charset in database to utf-8, utf8mb4, big5 and none of them works. i also tried to change my column from varchar to nvarchar but when i click apply (mysql workbench) the column is still "varchar".
this is my 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();
java mysql database hibernate encoding
java mysql database hibernate encoding
asked Nov 9 at 15:12
Danilo99
187
187
netbeans and browser can recognize chinese characters, only mysql show them as "???".
– Danilo99
Nov 9 at 15:17
Have you tried changing the font to a more utf8 friendly font? stackoverflow.com/questions/9553386/… en.wikipedia.org/wiki/Unicode_font
– Sedrick
Nov 9 at 15:31
I would tryGNU_Unifontin theResultset Grid:.
– Sedrick
Nov 9 at 15:38
i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLEkineski.kineskiCHANGE COLUMNkin_tekstkin_tekstNVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL
– Danilo99
Nov 9 at 15:46
Did you restart mysql workbench after the change?
– Sedrick
Nov 9 at 15:48
|
show 6 more comments
netbeans and browser can recognize chinese characters, only mysql show them as "???".
– Danilo99
Nov 9 at 15:17
Have you tried changing the font to a more utf8 friendly font? stackoverflow.com/questions/9553386/… en.wikipedia.org/wiki/Unicode_font
– Sedrick
Nov 9 at 15:31
I would tryGNU_Unifontin theResultset Grid:.
– Sedrick
Nov 9 at 15:38
i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLEkineski.kineskiCHANGE COLUMNkin_tekstkin_tekstNVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL
– Danilo99
Nov 9 at 15:46
Did you restart mysql workbench after the change?
– Sedrick
Nov 9 at 15:48
netbeans and browser can recognize chinese characters, only mysql show them as "???".
– Danilo99
Nov 9 at 15:17
netbeans and browser can recognize chinese characters, only mysql show them as "???".
– Danilo99
Nov 9 at 15:17
Have you tried changing the font to a more utf8 friendly font? stackoverflow.com/questions/9553386/… en.wikipedia.org/wiki/Unicode_font
– Sedrick
Nov 9 at 15:31
Have you tried changing the font to a more utf8 friendly font? stackoverflow.com/questions/9553386/… en.wikipedia.org/wiki/Unicode_font
– Sedrick
Nov 9 at 15:31
I would try
GNU_Unifont in the Resultset Grid:.– Sedrick
Nov 9 at 15:38
I would try
GNU_Unifont in the Resultset Grid:.– Sedrick
Nov 9 at 15:38
i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLE
kineski.kineski CHANGE COLUMN kin_tekst kin_tekst NVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL– Danilo99
Nov 9 at 15:46
i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLE
kineski.kineski CHANGE COLUMN kin_tekst kin_tekst NVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL– Danilo99
Nov 9 at 15:46
Did you restart mysql workbench after the change?
– Sedrick
Nov 9 at 15:48
Did you restart mysql workbench after the change?
– Sedrick
Nov 9 at 15:48
|
show 6 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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.
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. MySQL's utf8mb4 is a superset to MySQL's utf8.
Spring/Hibernate filter:
<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)
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
add a comment |
up vote
0
down vote
不知道你用的哪个版本的mysql, 但是你需要配置mysql
character_set_server=utf8mb4
一些版本hibernate能够正确配置,但是一些版本需要你手动配置。
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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.
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. MySQL's utf8mb4 is a superset to MySQL's utf8.
Spring/Hibernate filter:
<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)
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
add a comment |
up vote
1
down vote
accepted
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.
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. MySQL's utf8mb4 is a superset to MySQL's utf8.
Spring/Hibernate filter:
<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)
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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.
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. MySQL's utf8mb4 is a superset to MySQL's utf8.
Spring/Hibernate filter:
<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)
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.
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. MySQL's utf8mb4 is a superset to MySQL's utf8.
Spring/Hibernate filter:
<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)
answered Nov 9 at 23:33
Rick James
63.8k55593
63.8k55593
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
add a comment |
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
thank you very much. It finally works.
– Danilo99
Nov 9 at 23:45
add a comment |
up vote
0
down vote
不知道你用的哪个版本的mysql, 但是你需要配置mysql
character_set_server=utf8mb4
一些版本hibernate能够正确配置,但是一些版本需要你手动配置。
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
add a comment |
up vote
0
down vote
不知道你用的哪个版本的mysql, 但是你需要配置mysql
character_set_server=utf8mb4
一些版本hibernate能够正确配置,但是一些版本需要你手动配置。
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
不知道你用的哪个版本的mysql, 但是你需要配置mysql
character_set_server=utf8mb4
一些版本hibernate能够正确配置,但是一些版本需要你手动配置。
不知道你用的哪个版本的mysql, 但是你需要配置mysql
character_set_server=utf8mb4
一些版本hibernate能够正确配置,但是一些版本需要你手动配置。
answered Nov 9 at 15:34
Niuhuru Lang
10310
10310
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
add a comment |
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
where do i type "character_set_server=utf8mb4". Im sorry if it is a stupid question but im beginner when it comes to mysql and hibernate. can you please write in English because i cant read Chinese.
– Danilo99
Nov 9 at 16:21
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
sorry I thought you can read Chinese. Rick James 's answer works. And if you want to get more info, you can read the official document: dev.mysql.com/doc/refman/8.0/en/charset-unicode-sets.html and attention the version is the right one
– Niuhuru Lang
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
I will for sure, tanks.
– Danilo99
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228364%2fhow-to-write-chinese-caracters-into-mysql-with-hibernate%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
netbeans and browser can recognize chinese characters, only mysql show them as "???".
– Danilo99
Nov 9 at 15:17
Have you tried changing the font to a more utf8 friendly font? stackoverflow.com/questions/9553386/… en.wikipedia.org/wiki/Unicode_font
– Sedrick
Nov 9 at 15:31
I would try
GNU_Unifontin theResultset Grid:.– Sedrick
Nov 9 at 15:38
i tried that now and it still doesnt work. when i try to change column from varchar to nvarchar and in same tame set column collation "utf-8 default" it shows error. ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET 'utf8' NULL DEFAULT NULL' at line 2 SQL Statement: ALTER TABLE
kineski.kineskiCHANGE COLUMNkin_tekstkin_tekstNVARCHAR(4000) CHARACTER SET 'utf8' NULL DEFAULT NULL– Danilo99
Nov 9 at 15:46
Did you restart mysql workbench after the change?
– Sedrick
Nov 9 at 15:48