java inserts mysql garbled code
java inserts data into mysql and is divided into three layers:
● Front-end page
● Back-end code
● Database
Any layer of garbled code among these three layers will not work, so we need to face it Set the encoding format one by one in the three layers, and save the encoding uniformly without garbled characters.
Detailed explanation of three-layer encoding settings to solve the problem of garbled characters
1. Front-end
The front-end is to set the character set of the page
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2, background code
1)
a.Preferences (preference box) in Windows, look for workspace in general, there is text file encoding, change it to utf-8 (this is the character set modification for developing IDE)
b. Click the mouse on the top level of your project, then find the character encoding of this project in the project, and change it to utf- 8 (Character set modification of the project)
2)
The code must indicate the encoding type of request and response before obtaining the data (I use utf-8 here)
response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
After the setting is completed, you can get Chinese characters like this!
String name = new String(request.getParameter("name").getBytes("utf-8"), "utf-8"))
Use when connecting to the database:
jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf8
3. [mysqld] in the file of database
my.ini Set in the tag:
#做了如下添加 [client] default-character-set=utf8 character-set-server=utf8 init_connect='SET NAMES utf8'
With the above settings, the problem of garbled characters will no longer occur!
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of java inserts mysql garbled code. For more information, please follow other related articles on the PHP Chinese website!