java.sql.SQLException: 字符串值不正确: '\xF3\xBE\x8D\x81'

2022-09-04 20:45:54

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

Caused by: java.sql.SQLException: Incorrect string value: '\xF3\xBE\x8D\x81' for column 'twtText' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) at com.mysql.jdbc.sql.java.:2060) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922) at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) at 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;

答案 1

必须向列 添加字符集和归类。因此,您的列应如下所示:twtText

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

使用所需的排序规则进行更改。utf8_polish_ci

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

SHOW COLLATION;

答案 2

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

请参阅 9.1.10。统一码支持


推荐