Search code examples
javasqlprepared-statementnon-ascii-characters

Java: insert accented characters in mysql


If I have this query from java:

String query="insert into user (..., name, ...) values (..., 'à', ...)";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Spinning?user=root");
PreparedStatement prest = con.prepareStatement(query);
prest.executeUpdate();

In the db I will have a strange character: a diamond with a question mark inside.

Is there any solution to this problem?


Solution

  • Change your connection url to the following:

    jdbc:mysql://localhost/Spinning?user=root&useUnicode=true&characterEncoding=utf8