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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)