[JAVA] Check when double-byte characters are garbled when storing the information acquired by API etc. in the MySQL database.

Introduction

It is similar to a memo that the acquired double-byte characters are garbled and have been confirmed and resolved.

environment

Data obtained by using Java's Http Client I'm trying to connect to MySQL using a JDBC driver and put it in the DB.

That one

Did you cast the usual spell when you created the table? The usual spells are:

set character_set_client = utf8;
set character_set_connection = utf8;

CREATE TABLE `TABLE_NAME` (

(Abbreviation)

) DEFAULT CHARSET=utf8';

The point is to cast the spell before casting the SQL, If you're worried, cast the spell at the end of the CREATE TABLE statement.

To that

Are you casting a spell when building a request with HttpClient?

HttpClient client = HttpClient.newBuilder().version(Version.HTTP_2).build();
Builder requestBuilder = HttpRequest.newBuilder().uri(uri).setHeader("Content-type","application/json; charset=UTF-8");
HttpRequest request = requestBuilder.build();

The charset = UTF-8 behind setHeader is the spell.

That person

Are you casting spells in the JDBC driver settings?

dbconnection.properties


driver.class.name=com.mysql.jdbc.Driver
uri=jdbc:mysql://mysql.test.com/mydb?useUnicode=true&characterEncoding=utf8

The above mysql.test.com is the connection destination and mydb is the database name. Notice the subsequent ? UseUnicode = true & characterEncoding = utf8. I'm casting a spell.

at the end

In my case, that solved. I've implemented something similar in exactly the same environment, but at that time the characters weren't garbled ...

Recommended Posts

Check when double-byte characters are garbled when storing the information acquired by API etc. in the MySQL database.
[Java] Integer information of characters in a text file acquired by the read () method
What I did when JSF couldn't display database information in the view
Japanese characters described in MessageResources.properties are garbled