I'm struggling to find a solution to this encoding problem. I tried several alternatives found on the Internet/forums, but none of them seems to work.
Connection class
public class ConnectionFactory {
public Connection getConnection() {
try {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection("jdbc:mysql://localhost/fj21?useTimezone=true&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8", "root", "");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
On my JSP page I'm using the instructions:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
In "my.cnf" file I added:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysld]
default-character-set = utf8mb4
collation-server = utf8mb4_unicode_ci
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
character-set-database = utf8mb4
My MySQL variables are:
mysql> show variables like '%char%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.6.34-osx10.11-x86_64/share/charsets/ |
+--------------------------+---------------------------------------------------------+
mysql> show variables like '%coll%';
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8mb4_general_ci |
| collation_server | utf8mb4_general_ci |
+----------------------+--------------------+
And I still don't get the correct characters in the records ("ç", "ã", "á", etc.):
+----+----------------------+----------------------------+--------------------------------+----------------+
| id | name | email | address | dataNascimento |
+----+----------------------+----------------------------+--------------------------------+----------------+
| 25 | Conceição | con@email | Rua Conceição, 1 | 2020-10-04 |
+----+----------------------+----------------------------+--------------------------------+----------------
What am I missing?... :(