mysql插入数据报错 Incorrect string value: '\xF4\x8F\xAE\xB3

zhongzuo1981 2013-10-29 09:39:55
错误信息为
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。测试无效。

请高手指教,谢谢!!!

...全文
8932 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
onfire2020 2016-01-11
  • 打赏
  • 举报
回复
把setAsciiStream(5, fr, textFile.length());改成 pstmt.setCharacterStream(5, fr, textFile.length()); /** * DB地址 */ private static final String DB_URL="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf8";
zhongzuo1981 2013-10-31
  • 打赏
  • 举报
回复
结贴 在执行insert之前执行set names ‘utf8mb4’就可以了。
ACMAIN_CHM 2013-10-30
  • 打赏
  • 举报
回复
检查一下字符集设置。 http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx MySQL 中文显示乱码
zhongzuo1981 2013-10-29
  • 打赏
  • 举报
回复
引用 3 楼 WWWWA 的回复:
show variables like 'char%'; show create table xxx; 贴结果 JAVA代码是什么,在代码中转换字符集没有?
专家来吧…… 刚把字段类型改成longblob,可以写入了,在SQLyog上select正常,可在java里读出来的中文是乱码,各种转码也不好使。而且也不应该用longblog这个类型吧?
zhongzuo1981 2013-10-29
  • 打赏
  • 举报
回复
引用 3 楼 WWWWA 的回复:
show variables like 'char%'; show create table xxx; 贴结果 JAVA代码是什么,在代码中转换字符集没有?
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 tablename CREATE 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; }
WWWWA 2013-10-29
  • 打赏
  • 举报
回复
show variables like 'char%'; show create table xxx; 贴结果 JAVA代码是什么,在代码中转换字符集没有?
zhongzuo1981 2013-10-29
  • 打赏
  • 举报
回复
引用 1 楼 WWWWA 的回复:
show variables like 'char%'; show create table xxx; 连接MYSQL时指定字符集没有
指定了 jdbc:mysql://localhost/blog?useUnicode=true&characterEncoding=utf8
WWWWA 2013-10-29
  • 打赏
  • 举报
回复
show variables like 'char%'; show create table xxx; 连接MYSQL时指定字符集没有
oklinsong 2013-10-29
  • 打赏
  • 举报
回复
帮顶帮顶,专家给个思路
zhongzuo1981 2013-10-29
  • 打赏
  • 举报
回复
自己顶,自己顶。

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧