字符串值使用utf8编码的mysql中的错误

时间:2015-01-03 12:29:33

标签: mysql scala jdbc encoding hikaricp

我正在使用Scala与ScalikeJDBC,我的表编码是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' ''一次又一次,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:-1)

让你编码为UTF-8:

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

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