How to properly set utf8 encoding with jdbc and MySQL?

左心房为你撑大大i 提交于 2019-12-23 12:51:55

问题


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?


回答1:


Try to change your url like

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

The & must be represented as &




回答2:


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)


来源:https://stackoverflow.com/questions/33669612/how-to-properly-set-utf8-encoding-with-jdbc-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!