题设:
今天发布代码遇到一个奇怪的bug,在我的电脑上一切正常,使用的是本地数据库,移植到目标电脑上也能正常运行,但是有一个表查询不到数据,也不报异常。
调试:
1、运行系统,在设置中输入中文,发现返回乱码,猜测是乱码的问题
2、查看数据库编码:show variables like '%char%';发现有latin1编码,修改为utf8mb4,重启数据库,重启程序,仍然异常。(set character_set_result=utf8mb4;)
3、检查数据库配置连接:jdbc:mysql://39.106.157.61:3306/schmsite,未指定编码,于是修改成:jdbc:mysql://39.106.157.61:3306/schmsite?useUnicode=true&characterEncoding=utf8
4、运行正常。
以后和数据库打交道一定要首先保证编码格式正确,切记切记,浪费好久,而且是之前遇到过的问题。
贴上代码如下:
show VARIABLES like '%char%'; set character_set_client = utf8mb4; set character_set_connection = utf8mb4; set character_set_database = utf8mb4; set character_set_results = utf8mb4; set character_set_server = utf8mb4;