这是我的连接设置: Connection conn = DriverManager。getConnection(url + dbName + "?useUnicode=true&characterEncoding=utf-8",用户名,密码);

我得到以下错误时轮胎添加一行到一个表: 错误的字符串值:'\xF0\x90\x8D\x83\xF0\x90…在第1行为“content”列

我插入数千条记录,当文本包含\xF0时,我总是得到这个错误(即不正确的字符串值总是以\xF0开始)。

列的排序规则是utf8_general_ci。

有什么问题吗?


当前回答

同样的问题,要保存utf8mb4的数据,需要确保:

Character_set_client、character_set_connection、character_set_results为utf8mb4: Character_set_client和character_set_connection为客户端发送语句的字符集,character_set_results为服务器端返回查询结果给客户端的字符集。 看到charset-connection。 表和列编码为utf8mb4

对于JDBC,有两种解决方案:

解决方案一(需要重启MySQL):

修改my.cnf,重启MySQL: (mysql) default-character-set = utf8mb4 (mysqld) character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci

这可以确保数据库和character_set_client、character_set_connection、character_set_results默认为utf8mb4。

重新启动MySQL 将表和列编码更改为utf8mb4 停止在jdbc连接器中指定characterEncoding=UTF-8和characterSetResults=UTF-8,因为这将覆盖character_set_client, character_set_connection, character_set_results到utf8

解决方案二(不需要重启MySQL):

将表和列编码更改为utf8mb4 在jdbc连接器中指定characterEncoding=UTF-8,因为jdbc连接器不支持utf8mb4。 像这样写你的sql语句(需要添加allowMultiQueries=true到jdbc连接器): SET NAMES utf8mb4;INSERT INTO Mytable…

这将确保到服务器、character_set_client、character_set_connection、character_set_results的每个连接都是utf8mb4。 参见字符集连接。

其他回答

我还必须删除并重新创建数据库的所有存储过程(以及函数),以便它们在utf8mb4的新字符集中执行。

Run:

SHOW PROCEDURE STATUS;

查看哪些过程没有更新到服务器的新的character_set_client、collation_connection和Database Collation值。

如果您正在创建一个新的MySQL表,您可以在创建时指定所有列的字符集,这为我解决了这个问题。

CREATE TABLE tablename (
<list-of-columns>
)
CHARSET SET utf8mb4 COLLATE utf8mb4_unicode_ci;

你可以阅读更多详细信息:https://dev.mysql.com/doc/refman/8.0/en/charset-column.html

同样的问题,要保存utf8mb4的数据,需要确保:

Character_set_client、character_set_connection、character_set_results为utf8mb4: Character_set_client和character_set_connection为客户端发送语句的字符集,character_set_results为服务器端返回查询结果给客户端的字符集。 看到charset-connection。 表和列编码为utf8mb4

对于JDBC,有两种解决方案:

解决方案一(需要重启MySQL):

修改my.cnf,重启MySQL: (mysql) default-character-set = utf8mb4 (mysqld) character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci

这可以确保数据库和character_set_client、character_set_connection、character_set_results默认为utf8mb4。

重新启动MySQL 将表和列编码更改为utf8mb4 停止在jdbc连接器中指定characterEncoding=UTF-8和characterSetResults=UTF-8,因为这将覆盖character_set_client, character_set_connection, character_set_results到utf8

解决方案二(不需要重启MySQL):

将表和列编码更改为utf8mb4 在jdbc连接器中指定characterEncoding=UTF-8,因为jdbc连接器不支持utf8mb4。 像这样写你的sql语句(需要添加allowMultiQueries=true到jdbc连接器): SET NAMES utf8mb4;INSERT INTO Mytable…

这将确保到服务器、character_set_client、character_set_connection、character_set_results的每个连接都是utf8mb4。 参见字符集连接。

我的PLAY Java应用程序有这个问题。这是我对该异常的堆栈跟踪:

javax.persistence.PersistenceException: Error[Incorrect string value: '\xE0\xA6\xAC\xE0\xA6\xBE...' for column 'product_name' at row 1]
  at io.ebean.config.dbplatform.SqlCodeTranslator.translate(SqlCodeTranslator.java:52)
  at io.ebean.config.dbplatform.DatabasePlatform.translate(DatabasePlatform.java:192)
  at io.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:83)
  at io.ebeaninternal.server.persist.dml.DmlBeanPersister.insert(DmlBeanPersister.java:49)
  at io.ebeaninternal.server.core.PersistRequestBean.executeInsert(PersistRequestBean.java:1136)
  at io.ebeaninternal.server.core.PersistRequestBean.executeNow(PersistRequestBean.java:723)
  at io.ebeaninternal.server.core.PersistRequestBean.executeNoBatch(PersistRequestBean.java:778)
  at io.ebeaninternal.server.core.PersistRequestBean.executeOrQueue(PersistRequestBean.java:769)
  at io.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:456)
  at io.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:406)
  at io.ebeaninternal.server.persist.DefaultPersister.save(DefaultPersister.java:393)
  at io.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1602)
  at io.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1594)
  at io.ebean.Model.save(Model.java:190)
  at models.Product.create(Product.java:147)
  at controllers.PushData.xlsupload(PushData.java:67)
  at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$40(Routes.scala:690)
  at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:134)
  at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:133)
  at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$8$$anon$2$$anon$1.invocation(HandlerInvoker.scala:108)
  at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:88)
  at play.http.DefaultActionCreator$1.call(DefaultActionCreator.java:31)
  at play.core.j.JavaAction.$anonfun$apply$8(JavaAction.scala:138)
  at scala.concurrent.Future$.$anonfun$apply$1(Future.scala:655)
  at scala.util.Success.$anonfun$map$1(Try.scala:251)
  at scala.util.Success.map(Try.scala:209)
  at scala.concurrent.Future.$anonfun$map$1(Future.scala:289)
  at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
  at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:29)
  at scala.concurrent.impl.CallbackRunnable.run$$$capture(Promise.scala:60)
  at scala.concurrent.impl.CallbackRunnable.run(Promise.scala)
  at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:56)
  at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70)
  at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:48)
  at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:68)
  at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete(Promise.scala:368)
  at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete$(Promise.scala:367)
  at scala.concurrent.impl.Promise$KeptPromise$Successful.onComplete(Promise.scala:375)
  at scala.concurrent.impl.Promise.transform(Promise.scala:29)
  at scala.concurrent.impl.Promise.transform$(Promise.scala:27)
  at scala.concurrent.impl.Promise$KeptPromise$Successful.transform(Promise.scala:375)
  at scala.concurrent.Future.map(Future.scala:289)
  at scala.concurrent.Future.map$(Future.scala:289)
  at scala.concurrent.impl.Promise$KeptPromise$Successful.map(Promise.scala:375)
  at scala.concurrent.Future$.apply(Future.scala:655)
  at play.core.j.JavaAction.apply(JavaAction.scala:138)
  at play.api.mvc.Action.$anonfun$apply$2(Action.scala:96)
  at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:304)
  at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
  at scala.concurrent.impl.CallbackRunnable.run$$$capture(Promise.scala:60)
  at scala.concurrent.impl.CallbackRunnable.run(Promise.scala)
  at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
  at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
  at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
  at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:81)
  at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:91)
  at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
  at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(ForkJoinExecutorConfigurator.scala:43)
  at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
  at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
  at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
  at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.sql.SQLException: Incorrect string value: '\xE0\xA6\xAC\xE0\xA6\xBE...' for column 'product_name' at row 1
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
  at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
  at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
  at io.ebeaninternal.server.type.DataBind.executeUpdate(DataBind.java:82)
  at io.ebeaninternal.server.persist.dml.InsertHandler.execute(InsertHandler.java:122)
  at io.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:73)
  ... 59 more

我想用io.Ebean保存一条记录。我通过使用utf8mb4排序规则重新创建我的数据库来修复它,并应用play evolution重新创建所有表,以便所有表都应该使用utf-8排序规则重新创建。

CREATE DATABASE inventory CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

包含\xF0的字符串只是使用UTF-8编码为多个字节的字符。

尽管您的排序规则设置为utf8_general_ci,但我怀疑数据库、表甚至列的字符编码可能不同。它们是独立的设置。试一试:

ALTER TABLE database.table MODIFY COLUMN col VARCHAR(255)  
    CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL;

将实际数据类型替换为VARCHAR(255)