Mysql 设置utf-8 真正一劳永逸的方法

本文介绍了解决在使用MySQL时遇到的中文乱码问题的方法。通过修改MySQL配置文件my.cnf来设置字符集为UTF-8,并展示了具体的配置项。这样可以确保在进行insert和select操作时不会出现乱码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   用 bash 进入linux 之后,再 mysql -u root -p ,如果insert into 某个表,总是会出现乱码。

   网上搜了一堆,其中一个老外的说法才适用。用 set names utf8;在退出的时候又会不管用,所以

   要修改 mysql 的配置文件,刚刚开始脱离微软,觉得其他的世界都是配置文件,微软的就是傻瓜化

   一键操作。 vim /etc/mysql/my.cnf,找到 mysqld 配置节,在其后面追加这样的配置,下次再进来

  show variables like 'character_set_%' 就都是 utf8 了。insert 和 select 中文都不会乱码了。    

 

[mysqld] 
init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake
我的代码package hotel; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; /**jdbc快速入门demo*/ public class JDBCDemo2 { public static void main(String[] args) throws Exception { // Class.forName("com.mysql.jdbc.Driver"); //注册驱动 String url ="jdbc:mysql://127.0.0.1:3306/hotel?useSSL=false"; String username = "root"; String password = "Lqh2005320"; //获取链接 Connection conn = DriverManager.getConnection(url,username,password); //执行sql语句 String sql= "update room set stock = 15where id = 1"; Statement stmt = conn.createStatement();//获取对象statement int count = stmt.executeUpdate(sql); } } 出现错误D:\程序\bin\java.exe "-javaagent:C:\Users\moon\IntelliJ IDEA 2024.1.1\lib\idea_rt.jar=3640:C:\Users\moon\IntelliJ IDEA 2024.1.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\tem\jdbc\out\production\jdbc-demo;D:\tem\jdbc\jdbc-demo\lib\mysql-connector-java-8.0.11.jar hotel.JDBCDemo2 Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值