英文:
How to set UTF-8 encoding when displaying text from MySQL (JDBC,JSP)
问题
我在寻找解决这个编码问题的方法。我尝试了互联网/论坛上找到的几种替代方法,但似乎都没有起作用。
**Connection类**
```java
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);
}
}
}
在我的JSP页面中,我使用了以下指令:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
在“my.cnf”文件中,我添加了以下内容:
[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
我的MySQL变量如下:
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 |
+----------------------+--------------------+
但是记录中仍然没有得到正确的字符(“ç”,“ã”,“á”等):
+----+----------------------+----------------------------+--------------------------------+----------------+
| id | name | email | address | dataNascimento |
+----+----------------------+----------------------------+--------------------------------+----------------+
| 25 | Conceição | con@email | Rua Conceição, 1 | 2020-10-04 |
+----+----------------------+----------------------------+--------------------------------+----------------+
我漏掉了什么?...
<details>
<summary>英文:</summary>
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?...
:(
</details>
# 答案1
**得分**: 0
尝试将以下过滤器添加到您的项目中:
```java
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.
}
}
您可能还需要将以下内容添加到您的 web.xml 文件中:
<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>
英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论