Mysql插入数据 Incorrect string value: '\xF0\x9F\x98\x84

不知道什么情况先编辑的全部没有了

错误:不能向mysql插入4个和以上的字符,大多数是表情之类的比如:emoji表情

以前解决:是过滤emoji表情,但emoji表情ios android有些时候不同步,并且后面还有增加的可能,就有了以下解决方案

解决:

1.插入4个以下的

1.1查看unicode 和 utf-8编码对应

		// Unicode符号范围 | UTF-8编码方式
		// (十六进制) | (二进制)
		// --------------------+---------------------------------------------
		// 0000 0000-0000 007F | 0xxxxxxx
		// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
		// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
		// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

1.2过滤掉4个字节以上的字符

public static String fliterFourUnicode(String source) throws UnsupportedEncodingException {
	byte[] sourceBytes;

	sourceBytes = source.getBytes("utf-8");
	int subIndex = 0;
	String str = "";
	do {
		int curByte = Byte.toUnsignedInt(sourceBytes[subIndex]);
		if (curByte > 0x00 && curByte <= 0x7f) { //0xxxxxxx
			str = str + (char) sourceBytes[subIndex];
			subIndex++;
		} else if (curByte >= 0xc0 && curByte <= 0xdf) { //110xxxxx
			byte[] bytes = { sourceBytes[subIndex], sourceBytes[subIndex + 1] };
			str = str + new String(bytes, "utf-8");
			subIndex += 2;
		} else if (curByte >= 0xe0 && curByte <= 0xef) { //1110xxxx
			byte[] bytes = { sourceBytes[subIndex], sourceBytes[subIndex + 1], sourceBytes[subIndex + 2] };
			str = str + new String(bytes, "utf-8");
			subIndex += 3;
		} else if (curByte >= 0xf0 && curByte <= 0xf7) { //11110xxx
			str = str + "*";
			subIndex += 4;
		} else if (curByte >= 0xf8 && curByte <= 0xfb) { //111110xx
			str = str + "*";
			subIndex += 5;
		} else if (curByte >= 0xfc && curByte <= 0xfd) { //1111110x
			str = str + "*";
			subIndex += 6;
		} else if (curByte >= 0xfe) { //11111110
			str = str + "*";
			subIndex += 7;
		} else { //解析失败不是UTF-8编码开头字符
			return str + "*";
		}

	} while (subIndex < sourceBytes.length);
	return str;
}
2.让Mysql支持4个以上的,这种方案未测试过

2.1 修改字段类型为 varchar

2.2 修改utf8_general_ci 到utf8mb4_general_ci

2.3 去掉jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&characterEncoding=UTF-8中的&characterEncoding=UTF-8
让他自己适配,也可以升级高版本驱动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值