How to properly set utf8 encoding with jdbc and MySQL?

时间:2015-11-12 10:43:10

标签: java mysql jdbc encoding utf-8

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 个答案:

答案 0 :(得分:2)

Try to change your url like

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

The & must be represented as &

答案 1 :(得分:0)

...大概

  • 你有utf8编码的数据(好)
  • .content { background-color: #cccccc; height: 100%; margin: auto; /*opacity: 0.85;*/ padding: 10px; width: 80%; } .sub-menu { background: #2e2728 none repeat scroll 0 0; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2); left: 0; opacity: 0; padding: 0; position: absolute; top: 100%; transition: opacity 0.15s linear 0s; width: 100%; z-index: 999999; } 生效(默认,但错误)
  • 该列已声明{{1}}(默认,但错误)