0

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?... :(

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JSmith
  • 3
  • 4
  • Please don't add things like `[RESOLVED]` to titles. Instead **accept** the answer that helped you solve the problem, or - if there are no answers, or none helped you solve it - post your own solution and accept it after the timeout. Accepting is what marks a question as solved on Stack Overflow. – Mark Rotteveel Oct 21 '20 at 13:35
  • See "Mojibake" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Oct 24 '20 at 04:47
  • Sorry @MarkRotteveel ! I'm a newbie here! :) – JSmith Oct 29 '20 at 20:17

1 Answers1

0

Try adding this filter to your project:

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // NOOP.
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }



    @Override
    public void destroy() {
        // NOOP.
    }
}

You may also need to add this to your web.xml file

 <filter>
    <filter-name>CharacterEncodingFilter </filter-name>
    <filter-class>your-class-name</filter-class>
      <init-param>
        <param-name>CharacterEncodingFilter </param-name>
        <param-value>UTF-8</param-value>
      </init-param>
  </filter>

  <filter-mapping>
    <filter-name>CharacterEncodingFilter </filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jetski
  • 182
  • 2
  • 16