How to properly set utf8 encoding with jdbc and My

2019-06-26 03:29发布

JDBC and MySQL work just fine in my project except when it comes down to accented characters. This is the URL I use to access the database:

jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8

Suppose a resultSet = preparedStatement.executeQuery(), and then a System.out.println(resultSet.getString("text_with_accents"));. What's stored in the database is àèìòù (note that I've already set the right encoding in the database and all its tables), but what I get is ?????.

Is there a way to fix this?

2条回答
Lonely孤独者°
2楼-- · 2019-06-26 03:49

Probably...

  • you had utf8-encoded data (good)
  • SET NAMES latin1 was in effect (default, but wrong)
  • the column was declared CHARACTER SET latin1 (default, but wrong)
查看更多
Ridiculous、
3楼-- · 2019-06-26 04:02

Try to change your url like

url="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8" 

The & must be represented as &

查看更多
登录 后发表回答