Mysql插入数据报错java.sql.SQLException: Incorrect string value: 'xF0x9Fx93x8DxE6x88...'

今天读取solr里面的数据,往mysql插入时报错, Incorrect string value: 'xF0x9Fx93x8DxE8x88...' for column 'title' at row 1 

原因是标题有Emoj表情相关字符,因为我这边不需要保留Emoj表情,不需要复原显示,所以我选择了简单的方式,过滤字符。

                           byte[] b_text=title3.getBytes();
			    for (int i = 0; i < b_text.length; i++)  
			    {  
			        if((b_text[i] & 0xF8)== 0xF0){  
			            for (int j = 0; j < 4; j++) {                          
			            b_text[i+j]=0x30;                     
			        }  
			        i+=3;  
			        }  
			    }  
			    String title=new String(b_text);
title.replace("0000","")

在网上还看到别的代码,不过我没有试,你们可以看看

    /** 
      * 将emoji表情替换成空串 
      *   
      * @param source 
      * @return 过滤后的字符串 
      */  
     public static String filterEmoji(String source) {  
      if (source != null && source.length() > 0) {  
       return source.replaceAll("[ud800udc00-udbffudfffud800-udfff]", "");  
      } else {  
       return source;  
      }  
     }</span></span>  

  

原文地址:https://www.cnblogs.com/junrong624/p/6073700.html