Storing Special Character in MySQL - java

I am using mysql query browser to store the following names in the Person table which contains fields of personNumber and personName. I have the character set of personName at utf-8 and if i insert the name via query browse the query is running correctly but when i try that via JDBC or JPA, the name's special characters become the '?'. What is the problem here?...
The names are
1.Năstase
2.Hrustanović
3.Ogris-Martič and some similar names.

Have you set your connection string correctly?
jdbc:mysql://localhost:3306/administer?characterEncoding=utf8

Try this code
jdbc:mysql://localhost:3306/MY_DB?useUnicode=yes&characterEncoding=UTF8

Related

utf8_unicode_ci String is inserted incorrectly?

I have java application through which I do different operations on MySQL DB. The probleam is that when inserting utf8 String it is not inserted correctly. The charset of DB is utf8 and I have set collation to utf8_unicode_ci. Server connection collation is also utf8_unicode_ci. Furthermore when I insert data from phpMyAdmin it is inserted correctly, but when I do it from Java application using JOOQ - it is not. Example:
Result<ExecutorsRecord> executorsRecord =
context.insertInto(EXECUTORS, EXECUTORS.ID, EXECUTORS.NAME, EXECUTORS.SURNAME, EXECUTORS.REGION, EXECUTORS.PHONE, EXECUTORS.POINTS, EXECUTORS.E_TYPE)
.values(id, name, surname, region, phone, 0, type)
.returning(EXECUTORS.ID)
.fetch();
where name = "Бобр" and surname = "Добр", produces tuple with ???? as a name and ???? as surname. I have checked both strings, they are passed correctly to the method correctly.
As #spencer7593 suggested the problem could be in JDBC connector. So I added into url of connection following: ?characterEncoding=utf8 so that final url was "jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8", where mydb is a name of database. This has sorted out my problem. Also I would like to add the following statement (again by #spencer7593):
When we've got things configured correctly, and things aren't working, our goto suspect is the JDBC driver. To get timezone differences between the JVM and the MySQL database sorted out, to prevent the JDBC driver from "helping" by doing an illogical combination of various operations, we had to add two extra obscurely documented settings to the connection string.
Further reading

Getting rid of binary code from string before inserting row in MySQL database

I am fetching tweets from Twitter and storing them in a database for future use. I am using UTF-8 encoding in my driver, utf8_mb4_bin in my VARCHAR fields and utf8mb4_general_ciserver collation. The problem with that is that when inserting a value in a VARCHAR field, if the text has any binary code then it will throw an exception since VARCHAR utf8 does not accept binary.
Here is an example, I am fetching the text from here and try inserting it in my database and I get the error:
Incorrect string value: '\xF0\x9F\x98\xB1\xF0\x9F...' for column 'fullTweet' at row 1
My guess is that the two emoticons are causing this. How do I get rid of them before inserting the tweet text in my database?
Update:
Looks like I can manually enter the emoticons. I run this query:
INSERT INTO `tweets`(`id`, `createdAt`, `screenName`, `fullTweet`, `editedTweet`) VALUES (450,"1994-12-19","john",_utf8mb4 x'F09F98B1',_utf8mb4 x'F09F98B1')
and this is what the row in the table looks like:
You can remove non ascii characters from tweet string before inserting.
tweetStr = tweetStr.replaceAll("[^\\p{ASCII}]", "");
It looks like utf8mb4 support is still not configured correctly.
In order to use utf8mb4 in your fields you need to do the following:
Set character-set-server=utf8mb4 in your my.ini or my.cnf. Only character-set-server really matters here, other settings don't.
Add characterEncoding=UTF-8 to connection URL:
jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8
Configure collation of the field

Hibernate selects nothing

I have MySQL InnoDB table utf-8 encoded. This table has only id and name field. Names are in russian language and 1 name in English for testing purpose. English name selects ok but when trying to select with russian name it returns empty list. I tried to build queries both with Criterion and HQL.
getHibernateTemplate().find("from FirstName where name='free' ");
getHibernateTemplate().find("from FirstName where name='ИННА' ");
Here is connection string -
?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8
i'm using hibernate 3.2.7.ga and spring 2.5.6.SEC03
Here is hibernate log:
Hibernate: /* from FirstName where name='ИННА' */ select firstname0_.`id` as id1_24_, firstname0_.`name` as name2_24_ from `first_name` firstname0_ where firstname0_.`name`='ИННА' limit ?
Thanks to all, i have found an answer.
The problem was in connection string.
I did refactor and moved connection string to the properties file instead of xml with spring-bean. But i did not changed "&" entity to the "&".
Successfull connection string is in properties file
jdbc:mysql://${host}:${port}/${database}?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8
XML file changes & entities to the correct, but properties format don't do this.
Criteria coCriteria = Session.createCriteria(coClass).add(Restrictions.eq(strColumnName, strColumnValue));
list = coCriteria.list();
Did you try with this hibernate criteria.
Where "Session" will the session object of hibernate generated session.
1)coClass = "pojo class name"
2)strColumnName = "pojo class field name" in your case it will be "name" i guess
3)strColumnValue = in your case it will be "ИННА"
Did you try changing the character encoding at database level?
According to MySQL documentation, Cyrillic characters are supported with "cp1251"
You may take a look at this link as well

How to compare utf8 text in mysql query using java

I have a problem while selecting from a table containg data in utf-8 format in MySQL using java, in the WHERE clause I need to compare if a column value equals to a java String but they don't match
"Select age from student where name = '"+stringVariable+"';"
the stringVariable can sometimes be in arabic so in this case they don't match
The database is utf8mb4 and the connection also between java and database is utf_8 and I dont have a problem while inserting or just selecting data but the problem occurs while comparing
I tried to convert the string like this and it also didnt match
byte[] b = stringVariable.getBytes("UTF-8");
String str = new String(b,"UTF-8");
So anyone has any solution for that ?!
Thank you in advance
Use parameters. The driver should then encode them correctly according to the connection properties.
PreparedStatement statement = connection.prepareStatement(
"select age from student where name = ?");
statement.setString(1, stringVariable);
In addition, this also correctly escapes special SQL characters (like single quotes).
You shouldn't have to transform anything. The driver takes care of everything. Try using a prepared statement rather than string concatenation. This will have the additional advantage of avoiding SQL injection attacks, and make sure your statement works even if the string variable contains a quote.
I solved this problem it was not only related with the database query but I get the string from ajax request so I had to decode it from utf8 first and then get the parameter value using
URLDecoder.decode(request.getQueryString(), "UTF-8")
and then pass it to the query and I used prepared statement which encodes it according to the connection properties as you mentioned
Thank you

Execute Mysql query contains chinese character using java program

When I execute SQL query contains chines character, the query is executed successfully. When I open the mysql query browser I could see only ???? instead of Chinese texts. If the same query executed from the mysql query browser it works fine.
Try connecting with following switch
String url = "jdbc:mysql://host/database?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes";
All encoding must be the same (there are exceptions but let's forget them): the encoding used by the DB, the connection encoding use by the Java process and the one used by the query browser. You need to change the one used by Java, since the other two seem to be OK.

Categories