Problem with utf8 in java

I have the following table:

create table test ( fname char(20) character set utf8 collate utf8_turkish_ci, id int primary key ); 

I insert the data as follows:

 resultSet.executeQuery("set namee utf8"); preparedStatement = connection.prepareStatement("insert into test(fname) values(?)"); preparedStatement.setstring(1,"alis"); preparedStatement.executeUpdate(); 

But when I receive the data, they remind ????? .

What is a problem and how can I solve it?

+6
source share
1 answer

According to the MySQL JDBC documentation, you also need to set the character encoding to the JDBC connection URL. Here is an example:

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

Otherwise, the MySQL JDBC driver will use the standard platform encoding to convert characters to bytes before sending over the network, which in your case, apparently, is not UTF-8. All uncovered characters will be replaced by question marks.

In addition, when receiving data, you must make sure that the console / file in which you display / write characters also supports / uses UTF-8. Otherwise, they will also become question marks. How to fix this depends on how / where you display / write these characters.

See also:


By the way, you do not need a SET NAMES query.

+18
source

Source: https://habr.com/ru/post/886065/


All Articles