zoukankan      html  css  js  c++  java
  • java.sql.SQLException: Incorrect string value: 'xF0x9Fx91x88xE6x88...' for column 'content' at row 1

    往MySQL插入数据时,报错如下

    java.sql.SQLException: Incorrect string value: 'xF0x9Fx91x88xE6x88...' for column 'content' at row 1 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.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960) at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:388) at 

    因为emoji表情导致,有两种解决方法

    一、过滤掉字段中的emoji

    import org.apache.commons.lang.StringUtils;
    
    /**
     * @author: FengZhen
     * @create: 2019-02-25
     */
    public class EmojiFilter {
    
        private static boolean isEmojiCharacter(char codePoint) {
            return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
                    || (codePoint == 0xD)
                    || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                    || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                    || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
        }
    
        /**
         * 过滤emoji 或者 其他非文字类型的字符
         *
         * @param source
         * @return
         */
        public static String filterEmoji(String source) {
            if (StringUtils.isBlank(source)) {
                return source;
            }
            StringBuilder buf = null;
            int len = source.length();
            for (int i = 0; i < len; i++) {
                char codePoint = source.charAt(i);
                if (isEmojiCharacter(codePoint)) {
                    if (buf == null) {
                        buf = new StringBuilder(source.length());
                    }
                    buf.append(codePoint);
                }
            }
            if (buf == null) {
                return source;
            } else {
                if (buf.length() == len) {
                    buf = null;
                    return source;
                } else {
                    return buf.toString();
                }
            }
        }
    }

    二、将表编码设置为utf8mb4

    若需还原展示该字段,则建议此种方式。

  • 相关阅读:
    [Python]计算豆瓣电影TOP250的平均得分
    [Golang]使用自建代理访问指定网站
    HDU 2689.Sort it-冒泡排序
    HDU 1728.逃离迷宫-BFS
    hihoCoder #1498.Diligent Robots
    POJ 2503.Babelfish-sscanf()函数+strcmp()函数+二分
    Codeforces 608 B. Hamming Distance Sum-前缀和
    Codeforces 608 A. Saitama Destroys Hotel
    sscanf()函数
    UVA 11461.Square Numbers
  • 原文地址:https://www.cnblogs.com/EnzoDin/p/10431886.html
Copyright © 2011-2022 走看看