我使用下面的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
请帮忙
最佳答案
使用下面的代码解决了这个问题 ps.setString(4, new String("test".getBytes("ISO-8859-1"), "UTF-8"));
关于java在mysql中插入特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741338/