How to properly set utf8 encoding with jdbc and MySQL?

10,257

Try to change your url like

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

The & must be represented as &

Share:
10,257

Related videos on Youtube

pr0gma
Author by

pr0gma

Updated on June 18, 2022

Comments

  • pr0gma
    pr0gma almost 2 years

    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?

  • Rahul Tripathi
    Rahul Tripathi over 8 years
    @pr0gma:- Can you check your my.cnf and see if your file has [client] default-character-set=utf8 [mysql] default-character-set=utf8 present?
  • pr0gma
    pr0gma over 8 years
    I have no my.cnf. Should I create one? If so, where?
  • Rahul Tripathi
    Rahul Tripathi over 8 years
    @pr0gma:- It should be present. You can refer this: stackoverflow.com/questions/4292769/… to get its location
  • pr0gma
    pr0gma over 8 years
    Found the my.ini. Double checked, the option was already set in both the client and mysql section.
  • Rick James
    Rick James over 5 years
    characterEncoding=utf8 --> characterEncoding=UTF-8.