6

I have the following table:

create table test
(
  fname char(20) character set utf8 collate utf8_turkish_ci,
  id int primary key
);

I am inserting data as follows:

resultSet.executeQuery("set namee utf8");
preparedStatement = connection.prepareStatement("insert into test(fname) values(?)");
preparedStatement.setstring(1,"alis");
preparedStatement.executeUpdate();

But when I retrieve data they are resemble to ?????.

What is problem and how can I solve that?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
  • 2
    In your future questions, please pay attention to the code. The Java code which you've posted doesn't compile at all and that would only introduce lot of noise (red herrings) in the question. – BalusC Apr 16 '11 at 20:35

1 Answers1

20

As per the MySQL JDBC driver documentation you need to set the character encoding in the JDBC connection URL as well. Here's an example:

jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8

Otherwise the MySQL JDBC driver will use the platform default encoding to convert the characters to bytes before sending over network, which is in your case apparently not UTF-8. All uncovered characters will then be replaced by question marks.

Also, when retrieving the data, you need to ensure that the console/file where you're displaying/writing the characters to also supports/uses UTF-8. Otherwise they will become question marks as well. How to fix that depends on how/where you're displaying/writing those characters to.

See also:


By the way, you don't need the SET NAMES query here.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I used the characterEncoding=UTF-8 attribute only so far. What does the useUnicode=yes does? – Rihards Apr 16 '11 at 20:34
  • @richards: Just to be sure. It indeed defaults to yes/true. But if this is changed somewhere/somehow, then alone `characterEncoding=UTF-8` will still fail. – BalusC Apr 16 '11 at 20:38
  • Two times in a row in a single day, your old answers saved me a lot of time. +6 and thanks! – Goran Jovic Jan 13 '12 at 15:26