Dear, I have a small problem with a Java application that I am writing.
It is an application that connects via JDBC to MySQL. When saving the records, the characters are saved correctly (if I write "Mexico" in a JText box and send it to the database, it is saved as "Mexico"); but when reading them back using the ResultSet.getString()
method the "special" characters (accents and "ñ") appear wrong ("Mexico" reads "Mexico").
I think it's something that has to do with the "encoding" of the characters, but I do not know specifically what it is .. The MySQL database uses the encoding utf8_spanish_ci
, and the function Charset.defaultCharset()
returns UTF-8
.
My specific question is, then: How to get MySQL read strings that contain special characters (that were correctly stored) to be displayed correctly in the Java application?
Update (partial solution):
After looking for a while, I found this question and its answer that helped me. Specifically, what it says is that when you open the connection you have to specify the set of characters that will be used; in my case:
DriverManager.getConnection(
"jdbc:mysql://" + host + "/" + dbName
+ "?useUnicode=true&characterEncoding=utf8_spanish_ci",
user, pass);
However, it only solves partially the problem:
When reading data from fields VARCHAR
the special characters are read correctly. However, when reading fields JSON
, values that have special characters are still shown "rare".
Update (final):
The problem has to do with the encoding that MySQL uses to save the JSON data; The answer below illustrates the procedure I followed to solve it.