user1613831
user1613831

Reputation:

UTF-8 Queries with JDBC

i want to ask the MYSQL an UTF-8 Query but it does not work fine . when i try the following query , the result comes up truly :

String query = "select * from Terms where Term = 'lol'";

but with the following query doesn't make a response :

String query = "select * from Terms where Term = 'خدابخش'";

where the

'خدابخش'

part is in Persian and UTF-8 . note that the connection to the database is fine .

Upvotes: 1

Views: 2779

Answers (2)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

Chances are that you may need to set your character encoding in your JDBC connection. If you are using MySQL JDBC Connector you do it using the property characterEncoding. Somewhat like this:

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

You may want to read the reference on encoding and character sets in your connector JDBC documentation.

This is the one that mentions the use of characterEncoding for the MySQL JDBC Connector:

Connector JDBC: Using Character Sets and Unicode

Upvotes: 4

Isaac
Isaac

Reputation: 16736

One or more of the following is true:

  1. The Java compiler, compiling your code, is set to read the source file with a different encoding in which the source file was actually stored. In other words, there is a discrepancy between the encoding that your editor uses, the encoding in which the file is actually saved, and the encoding with which the Java compiler is reading your source code.
  2. Your database isn't set correctly to accept/store Unicode characters. Ensure that your database is set correctly. Looks like you're using MySQL. You may want to create a dump of the database using mysqldump and witness how the database was created with respect to character sets.

Upvotes: 0

Related Questions