1

我正在使用带有 ScalikeJDBC 的 scala,我的表编码是 utf8mb4 和 utf8mb4_general_ci 集合,我得到:

03/01/2015 08:40:57 - ERROR[http-apr-8080-exec-9] StatementExecutor$$anon$1 - SQL execution failed (Reason: Incorrect string value: '\xF0\x9F\x98\x8A' for column 'body' at row 81):

即使我的数据源代码是:

val dataSource: DataSource = {
    val ds = new HikariDataSource()
    ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
    ds.addDataSourceProperty("url", "jdbc:mysql://" + org.Server.GlobalSettings.DB.mySQLIP + ":3306?rewriteBatchedStatements=true")
    ds.addDataSourceProperty("user", "SOMEUSER")
    ds.addDataSourceProperty("password", "SomePassword")
    ds.setAutoCommit(false) // dosne't work

    ds.addDataSourceProperty("cachePrepStmts", "true")
    ds.addDataSourceProperty("prepStmtCacheSize", "250")
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048")
    ds.addDataSourceProperty("useServerPrepStmts", "true")
    ds.addDataSourceProperty("useUnicode", "yes")
    ds.addDataSourceProperty("characterEncoding", "utf8")
    ds.addDataSourceProperty("connectionCollation", "utf8_unicode_ci")

    //      ds.addDataSourceProperty("characterEncoding", "utf8mb4") //dosne't work either
    //      ds.addDataSourceProperty("connectionCollation", "utf8mb4_unicode_ci")//dosne't work either
    ds
  }

对于正文栏,我这样做:

 try{
    .
    .
    .
        body = new String(rev.body.getBytes("utf-8"), "utf-8") 
    .
    .
    .
    }

没有发现异常,但仍然在查询提交时失败:''不正确的字符串值:......对于列'body''',一次又一次,有人可以帮助我吗?

4

1 回答 1

-1

让你编码为 UTF-8:

body = new String(rev.body.getBytes("utf-8"), "UTF-8")

或使用java.nio.charset.StandardCharsets.UTF_8.

于 2015-01-03T12:38:02.963 回答