javajdbcutf-8character-encoding

Java PreparedStatement UTF-8 character problem


I have a prepared statement:

PreparedStatement st;

In my code, I try to use st.setString method.

st.setString(1, userName);

The value of userName is şakça. The setString method changes 'şakça' to '?akça'. It doesn't recognize UTF-8 characters. How can I solve this problem?


Solution

  • The number of ways this can get screwed up is actually quite impressive. If you're using MySQL, try adding a characterEncoding=UTF-8 parameter to the end of your JDBC connection URL:

    jdbc:mysql://server/database?characterEncoding=UTF-8

    You should also check that the table / column character set is UTF-8.