Java – get question mark when inserting Hebrew characters into MySQL table

I am using NetBeans, using java to build web applications, and JSP uses Hebrew fields to process databases

DDL is as follows:

String cityTable = "CREATE TABLE IF NOT EXISTS hebrew_test.table ("
                            +"id int(11) NOT NULL AUTO_INCREMENT,"
                            +"en varchar(30) NOT NULL,"
                            +"he varchar(30) COLLATE utf8_bin NOT NULL,"
                            +"PRIMARY KEY (id)"
                            +") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;";
String insert = "INSERT INTO hebrew_test.table (en,he) VALUES ('A','a')";
String insert2 = "INSERT INTO hebrew_test.table (en,he) VALUES ('B','ב')";
String insert3 = "INSERT INTO hebrew_test.table (en,he) VALUES ('C','אבג')";


executesqlCommand(cityTable);
executesqlCommand(insert);
executesqlCommand(insert2);
executesqlCommand(insert3);

Output table I got:

1   A   a
2   B   ?
3   C   ???

Substitute:

1   A   a
2   B   ב
3   C   אבג

I've tried Hebrew appearances as question marks in NetBeans, but that's not the same problem I got a question mark in the form

I also define the table as utf8_ Bin, as shown in the code above

Solution

You need to tell the jdbc driver to use UTF - 8 encoding and decode the characters representing the SQL query into bytes You can do this by adding the useunicode = yes and characterencoding = UTF - 8 query parameters to the JDBC connection URL

jdbc:MysqL://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8

Otherwise, it will use the operating system platform default character set The MySQL jdbc driver itself knows very well the codes used in the client (where the JDBC code runs) and server (where the DB table is located) Any character not covered by the character set used in the DB table will be replaced by a question mark

You can also see:

> Spring Encoding with CharacterEncodingFilter in web. xml

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>