jdbc - 无法在mysql中存储俄语中文阿拉伯语字母

时间:2015-04-20 23:55:40

标签: mysql jdbc utf-8 utf8mb4

我已经详尽地搜索了stackoverflow和mysql文档中的几乎所有资源,但我无法使用JDBC插入俄语文本。我非常感谢您对我所缺少的任何指导。请在下面找到两种方法。

方法1:

  1. 使用utf8创建数据库

    创建数据库apprev DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

  2. 使用utf8字符集创建表

    CREATE TABLE reviewsid int(11)NOT NULL AUTO_INCREMENT,
    date日期NOT NULL DEFAULT'0000-00-00',
    appstr varchar(45)NOT NULL,
    appid varchar(45)NOT NULL,
    appname varchar(45)NOT NULL,
    country varchar(45)DEFAULT NULL,
    version varchar(20)DEFAULT NULL,
    reviewer varchar(100)DEFAULT NULL,
    rev_rating varchar(20)DEFAULT NULL,
    rev_title varchar(500)DEFAULT NULL,
    rev_text varchar(4000)DEFAULT NULL,
    last_upd_ts时间戳NOT NULL DEFAULT CURRENT_TIMESTAMP  ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY(id),
     )ENGINE = InnoDB DEFAULT CHARSET = utf8;

  3. 这是我用于使用JDBC

    插入数据的连接字符串

    JDBC:MySQL的:// XXX:3306 / apprev useServerPrepStmts =假安培; rewriteBatchedStatements =真安培;了useUnicode =真安培;的characterEncoding = UTF-8

    pstmt.setString(7,obj.get(“reviewer”)。toString()。replace(“\”“,”“)); pstmt.setString(9,obj.get(“title”)。toString()。replace(“\”“,”“));

  4. 此错误失败。

    java.sql.BatchUpdateException: Incorrect string value: '\xF0\x9F\x91\x8D' for column 'rev_title' at row 6
            at com.mysql.jdbc.PreparedStatement.executeBatchedInserts(PreparedStatement.java:1836)
            at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1456)
            at loaddata.LoadAppData.loadReviews(LoadAppData.java:256)
            at loaddata.LoadAppData.main(LoadAppData.java:557)
    Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x8D' for column 'rev_title' at row 6
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    

    方法2: 现在,在引用一些stackoverflow线程后,使用utf8mb4重复这些步骤。

    1. create database apprev DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

    2. 创建表reviews
      id int(11)NOT NULL AUTO_INCREMENT,
      date日期NOT NULL DEFAULT'0000-00-00',
      appstr varchar(45)NOT NULL,
      appid varchar(45)NOT NULL,
      appname varchar(45)NOT NULL,
      country varchar(45)DEFAULT NULL,
      version varchar(20)DEFAULT NULL,
      reviewer varchar(100)CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
      rev_rating varchar(20)DEFAULT NULL,
      rev_title varchar(500)CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
      rev_text varchar(4000)CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL,
      last_upd_ts时间戳NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY(id),
      )ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

    3. 连接字符串如下所述

    4. JDBC:MySQL的:// XXX:3306 / apprev useServerPrepStmts =假安培; rewriteBatchedStatements =真安培;了useUnicode =真安培;被character_set_server = utf8mb4

      我现在能够插入。但我只看到“????????”当我使用jdbc加载数据时,俄语字符。

      Mysql版本是5.6.14-ndb-7.3.3-cluster-gpl

      请让我知道我错过了什么。

0 个答案:

没有答案