Mysql 插入与编码

本文详细探讨了MySQL插入与编码时如何防止中文乱码的问题,重点介绍了character_set_client、character_set_connection和character_set_results的重要性,强调这三个编码需保持一致。通过设置`set names utf8mb4`来确保数据传输过程中不丢失信息,特别是在binlog同步场景中,正确处理不同字符集间的转换,以保证数据完整性和正确性。
摘要由CSDN通过智能技术生成

测试1

目标库编码:utf8mb4
目标表:

CREATE TABLE `a` (
  `id` int(11) NOT NULL,
  `value` mediumtext,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
@Test
    public void test() throws Exception {
   
        String s = "中文";
        byte[] gbkBytes = s.getBytes("GBK");
        byte[] utf8Bytes = s.getBytes("UTF-8");

		// gbkBytes.length == 4
		// utf8Bytes.length == 6

        // 输出乱码:����
        System.out.println("gbk Bytes decode by UTF-8");
        System.out.println(new String(gbkBytes, "UTF-8"));

        // 输出:中文
        System.out.println("gbk Bytes decode by GBK");
        System.out.println(new String(gbkBytes, "GBK"));

        // 输出:中文
        System.out.println("utf8 Bytes decode by UTF-8");
        System.out.println(new String(utf8Bytes, "UTF-8"));

        // 输出乱码:涓枃
        System.out.println("utf8 Bytes decode by GBK");
        
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值