【问题标题】:java.sql.SQLException: Incorrect string value: '\xF3\xBE\x8D\x81'java.sql.SQLException:不正确的字符串值:'\xF3\xBE\x8D\x81'
【发布时间】:2011-03-23 13:45:44
【问题描述】:

我在尝试保存一些推文时遇到以下异常,

原因:java.sql.SQLException:不正确的字符串值:第 1 行的列 'twtText' 的'\xF3\xBE\x8D\x81' 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 在 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) 在 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) 在 com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) 在 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) 在 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) 在 com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922) 在 org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)

我的表结构如下,所有的列都是UTF-8格式,

 CREATE TABLE `tblkeywordtracking` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `word` varchar(200) NOT NULL,
  `tweetId` bigint(100) NOT NULL,
  `twtText` varchar(800) DEFAULT NULL,
  `negTwtText` varchar(1000) DEFAULT NULL,
  `language` text,
  `links` text,
  `negWt` double DEFAULT NULL,
  `posWt` double DEFAULT NULL,
  `tweetType` varchar(20) DEFAULT NULL,
  `source` text,
  `sourceStripped` text,
  `isTruncated` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
  `inReplyToStatusId` bigint(30) DEFAULT NULL,
  `inReplyToUserId` int(11) DEFAULT NULL,
  `isFavorited` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
  `inReplyToScreenName` varchar(40) DEFAULT NULL,
  `latitude` bigint(100) NOT NULL,
  `longitude` bigint(100) NOT NULL,
  `retweetedStatus` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
  `statusInReplyToStatusId` bigint(100) NOT NULL,
  `statusInReplyToUserId` bigint(100) NOT NULL,
  `statusFavorited` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
  `statusInReplyToScreenName` text,
  `screenName` text,
  `profilePicUrl` text,
  `twitterId` bigint(100) NOT NULL,
  `name` text,
  `location` text,
  `bio` text,
  `utcOffset` int(11) DEFAULT NULL,
  `timeZone` varchar(100) DEFAULT NULL,
  `frenCnt` bigint(20) DEFAULT '0',
  `createdAt` datetime DEFAULT NULL,
  `createdOnGMT` text CHARACTER SET latin1,
  `createdOnServerTime` datetime DEFAULT NULL,
  `follCnt` bigint(20) DEFAULT '0',
  `favCnt` bigint(20) DEFAULT '0',
  `totStatusCnt` bigint(20) DEFAULT NULL,
  `usrCrtDate` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `id` (`id`,`word`),
  KEY `twtText` (`twtText`(333)),
  KEY `word` (`word`,`tweetType`),
  KEY `posWt` (`posWt`,`negWt`)
) ENGINE=MyISAM AUTO_INCREMENT=1740 DEFAULT CHARSET=utf8;

【问题讨论】:

  • 您必须提供有关您的方案中使用的 MySQL 服务器和 MySQL 连接器版本的信息。

标签: java mysql utf-8


【解决方案1】:

您必须将字符集和排序规则添加到列twtText。所以你的专栏应该是这样的:

twtText varchar(800) character set utf8 collate utf8_polish_ci DEFAULT NULL,

用你想要的排序规则更改utf8_polish_ci

运行以下查询以查看可用的排序规则:

SHOW COLLATION;

【讨论】:

  • 如果您使用的是 MySQL >= 5.5,最好是 utf8mb4utf8 只存储 1-3 字节的 UTF8 字符。
【解决方案2】:

MySQL 5.0/5.1 不支持 4 字节 UTF8 字符,这是一个已知限制。 MySQL 5.5 确实支持 4 字节 UTF8 字符。

9.1.10. Unicode Support

【讨论】:

    【解决方案3】:

    它看起来像是一个有效的 utf-8 序列,它编码了以下字符 U+FE341

    如您所见,这是一个使用超过 2 个字节的 Unicode 字符。从 thisthis 我推断 MySQL 仍然不支持这个 Unicode 字符子集(至少对于版本

    【讨论】:

    • 3 字节 UTF8 序列 == 2 字节 Unicode 字符。这是一个 4 字节的 UTF8 序列,它编码 20 位 Unicode 字符。
    • 你是对的,我错过了 UTF8 编码的额外位。 0xF3BE8D81 是 11110 011 10 111110 10 001101 10 000001 这是 Unicode 011111110001101000001 = 0xFE341。
    猜你喜欢
    • 2017-05-27
    • 2019-05-27
    • 2018-05-05
    • 2013-02-10
    • 2015-06-01
    • 1970-01-01
    • 2012-11-19
    • 2011-10-05
    • 2016-11-27
    相关资源
    最近更新 更多