【问题标题】:java insert special characters in mysqljava在mysql中插入特殊字符
【发布时间】:2016-04-16 22:20:32
【问题描述】:

我使用下面的java代码在mysql中插入数据

private Long insertMFPatient() {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(
                Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(
                    "insert into test(firstname, lastname) values(?,?)",
                    new String[] { "changeid" });
            ps.setString(1, "mamá");
            ps.setString(2, "test2");
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().longValue();

}

在插入像 mamá 这样的文本时,存储在 mysql 中的数据是 mamá¡ 我应该怎么做才能纠正这个? 迄今为止 1.我已将表格编码类型更新为UTF-8 2. 更新了连接字符串 jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

请帮忙

【问题讨论】:

  • 正如您的输出所示,á 正确存储为 UTF-8 - 以两个字节的序列。请显示您用于读取/显示该信息的代码 - 错误很可能存在。
  • 我可以在数据库中看到不正确的文本。错误是在插入时而不是在读取时

标签: java spring utf-8


【解决方案1】:

使用下面的代码解决了 ps.setString(4, new String("test".getBytes("ISO-8859-1"), "UTF-8"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2016-05-12
    • 2016-11-24
    相关资源
    最近更新 更多