错误信息为
java.sql.SQLException: Incorrect string value: '\xF4\x8F\xAE\xB3</...' for column 'contenthtml' at row 1共25w数据,有74条报这个错误,数据中有中文及特殊字符,字段数据类型longtext,字符集utf8,校验utf8_general_ci。发现把写不进去的内容粘贴到记事本里,保存一下,再在客户端粘贴到字段里就可以保存上。网上查了一些,说把utf8改为utf8mb4。测试无效。请高手指教,谢谢!!!

解决方案 »

  1.   

    show variables like 'char%'; 
    show create table xxx;
    连接MYSQL时指定字符集没有
      

  2.   


    指定了
    jdbc:mysql://localhost/blog?useUnicode=true&characterEncoding=utf8
      

  3.   

    show variables like 'char%'; 
     show create table xxx;
    贴结果
    JAVA代码是什么,在代码中转换字符集没有?
      

  4.   


    show variables like 'char%'; 
    Variable_name Value
    character_set_client utf8
    character_set_connection utf8
    character_set_database utf8
    character_set_filesystem binary
    character_set_results utf8
    character_set_server utf8
    character_set_system utf8
    character_sets_dir /data/mysql/share/charsets/show create table tablenameCREATE TABLE `tablename` (
      ......
      `contenthtml` longtext CHARACTER SET utf8mb4,
      `contenttext` longtext CHARACTER SET utf8mb4,
      ......
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    数据是从oracle读的,clob类型字段。
    java代码
    public static void selectAndInsert(String readExp, String insertExp, Map<String, String> columns){
    Connection connOracle = null;
    PreparedStatement stmtOracle = null;
    ResultSet rsOracle = null;
    Connection connMysql = null;
    PreparedStatement stmtMysql = null;
    String id = "";
    try{
    connOracle = DataSource.getOracleConnection();
    stmtOracle = connOracle.prepareStatement(readExp, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    logger.info("正在读取数据");
    rsOracle = stmtOracle.executeQuery();

    connMysql = DataSource.getMysqlConnection();
    stmtMysql = connMysql.prepareStatement(insertExp);
    rsOracle.last(); 
    logger.info("数据条数:"+rsOracle.getRow());
    int c = 1;
    rsOracle.beforeFirst();
    while(rsOracle.next()){
    id = String.valueOf(rsOracle.getInt("ID"));
    for(Map.Entry<String, String> entry : columns.entrySet()){
    int pIndex = Integer.valueOf(entry.getValue()).intValue();
    if(entry.getKey().equalsIgnoreCase("contenttext")){
    stmtMysql.setString(pIndex, DataUtil.loadContentTEXT(rsOracle));
    }else if(entry.getKey().equalsIgnoreCase("contenthtml")){
    stmtMysql.setString(pIndex, DataUtil.loadContentHTML(rsOracle));
    }else{
    Object obj = rsOracle.getObject(entry.getKey());
    if(obj instanceof BigDecimal){
    stmtMysql.setInt(pIndex, ((BigDecimal) obj).intValue());
    }else if(obj instanceof String){
    stmtMysql.setString(pIndex, obj.toString());
    }else if(obj instanceof Date){
    Timestamp timestamp = rsOracle.getTimestamp(entry.getKey());
    stmtMysql.setTimestamp(pIndex, timestamp);
    }else{
    stmtMysql.setObject(Integer.valueOf(entry.getValue()).intValue(), null);
    }
    }
    }
    try{
    stmtMysql.executeUpdate();
    c++;
    if(c%1000==0){
    logger.info("写入"+c+"条");
    }
    }catch(SQLException sqlEx){
    logger.error("写入失败,原数据id: "+id, sqlEx);
    }
    }
    }catch(Exception e){
    logger.error("导入失败", e);
    }finally{
    DataSource.closeDBSrc(connOracle, stmtOracle, rsOracle);
    DataSource.closeDBSrc(connMysql, stmtMysql, null);
    }
    }public static String loadContentHTML(ResultSet rs) throws Exception {
    Reader reader = rs.getCharacterStream("CONTENTHTML");
    if (reader != null) {
    try {
    int len = 0;
    char[] cbuf = new char[8192];
    StringBuffer sb = new StringBuffer(8192);
    while ((len = reader.read(cbuf)) != -1) {
    sb.append(cbuf, 0, len);
    }
    return new String(sb.toString());
    } catch (Exception e) {
    throw e;
    } finally {
    try {
    reader.close();
    } catch (Exception e) {
    // Ignore
    }
    }
    }
    return null;
    }
      

  5.   


    专家来吧……刚把字段类型改成longblob,可以写入了,在SQLyog上select正常,可在java里读出来的中文是乱码,各种转码也不好使。而且也不应该用longblog这个类型吧?
      

  6.   

    检查一下字符集设置。http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx
    MySQL 中文显示乱码
      

  7.   

    结贴
    在执行insert之前执行set names ‘utf8mb4’就可以了。