5

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?

pr0gma
  • 577
  • 3
  • 8
  • 18

2 Answers2

9

Try to change your url like

url="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8" 

The & must be represented as &

Fahim Farook
  • 1,482
  • 2
  • 14
  • 38
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • @pr0gma:- Can you check your my.cnf and see if your file has `[client] default-character-set=utf8 [mysql] default-character-set=utf8` present? – Rahul Tripathi Nov 12 '15 at 10:49
  • I have no my.cnf. Should I create one? If so, where? – pr0gma Nov 12 '15 at 10:52
  • @pr0gma:- It should be present. You can refer this: http://stackoverflow.com/questions/4292769/what-is-the-location-of-mysql-client-my-cnf-in-xampp-for-windows to get its location – Rahul Tripathi Nov 12 '15 at 10:59
  • Found the my.ini. Double checked, the option was already set in both the client and mysql section. – pr0gma Nov 12 '15 at 16:41
  • 1
    `characterEncoding=utf8` --> `characterEncoding=UTF-8`. – Rick James Dec 23 '18 at 20:06
  • Is there any change to set those attributes at runtime at DataSource creation instead of changing connection string? – Tobia Aug 25 '22 at 08:15
0

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)
Rick James
  • 135,179
  • 13
  • 127
  • 222