无法将俄语插入mysql

时间:2018-01-01 05:17:25

标签: java mysql

1。显示'%character%set%';

等变量

see image

2. 我的表:

CREATE TABLE `t_languagetable` 
         ( 
                      `id`           INT(11) NOT NULL, 
                      `languagecode` VARCHAR(2) NOT NULL, 
                      `countrycode`  VARCHAR(2) NULL DEFAULT NULL, 
                      `variant`      VARCHAR(10) NULL DEFAULT NULL, 
                      `language`     VARCHAR(50) NOT NULL, 
                      `displayname`  VARCHAR(50) NOT NULL collate 
                      'utf8_general_ci', 
                      PRIMARY KEY (`id`) 
         ) 
         COLLATE='latin1_swedish_ci' engine=innodb ;

3. 我的java代码:

    Connection connect = null;
    Statement statement = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");

        connect = DriverManager.getConnection("jdbc:mysql://192.168.99.211/"
                + databaseName + "?"
                + "user=" + username
                + "&password=" + password);

        statement = connect.createStatement();
        String displayName = "Русский";

        String insertSql = "INSERT INTO t_languagetable " +
                "VALUES (" + 1 + ", 'va', null, 'null', 'null' ," + "'" + displayName + "'" + ")";
        statement.executeUpdate(insertSql);
        System.out.println("insertSql: " + insertSql);
        connect.close();
    } catch (Exception e) {
        System.out.println("problem during the connection with the database!"+e);
    }

4. 结果:

russian character display as question mark

1 个答案:

答案 0 :(得分:1)

解决。

您已尝试在setNString中的插入时间displayname更改方法,不要在数据库中更改...!

尝试这种方式

Connection connect = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1/"+databaseName+"?user="+username+"&password="+password);          
            PreparedStatement prep = connect.prepareStatement("INSERT INTO t_languagetable (id,languagecode,displayname) VALUES (?,?,?)");
            prep.setInt(1,7);
            prep.setString(2,"AA");
            prep.setNString(3,"Русский");

            prep.execute();
            System.out.println("Data Inserted.");
            connect.close();
        } catch (Exception e) {
            System.out.println("problem during the connection with the database!"+e);
        }