Martina
Martina

Reputation: 1918

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?

Upvotes: 3

Views: 2056

Answers (2)

Wim Deblauwe
Wim Deblauwe

Reputation: 26858

Change your connection url to the following:

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

Upvotes: 3

Bimalesh Jha
Bimalesh Jha

Reputation: 1504

  1. Verify the character set you are using in MySQL DB. You can try "SHOW CREATE TABLE xxxx" to print the table DDL with charset being used.
  2. Verify the character set you are using in JDBC driver. If using MySQL ConnectorJ, you can set charset in the JDBC url.

Upvotes: 1

Related Questions