0

In my db, there is a table with a column that may contain Unicode characters that are entered by end users. I have configured my hibernate mysql connection in a property file as given below.

spring.datasource.url: jdbc:mysql://localhost:3306/my_db?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

For DB table and the column, I use utf8mb4 charset with utf8mb4_unicode_ci collation.

Above configuration works fine for inserting into the table that has the column I mentioned above. user can enter the unicode characters from the UI and they get saved fine in the table.

Then, there is this big join query I use to retrieve some set of user data which includes aforementioned Unicode data too. There, at the runtime, I get a MySql grammar error.

In the query, I check if the search param is null. That is where the exception is thrown from when the param is null.

"..... where (:fromDate is null or bt.created_date >= :fromDate) and (:toDate is null or bt.created_date <= :toDate) and ...... "

Error log:

................... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where (null is null or bt.created_date >= null) ........

I tried adding &characterSetResults=UTF-8 also to the connection string with no luck.

What is the issue here. How can I get this fixed.

EDIT:

It turned out that the issue has nothing to do with the join I have mentioned. Even without the table with unicode data, issue still happens. When unicode configs are removed from the connection strings only, it works fine.

May be a issue with mismatching charsets and collations between db and client ?

6
  • you can't put placeholders on the left sight
    – nbk
    Jul 5, 2020 at 16:32
  • It's complaining at WHERE or just before that. So, please show us the earlier part of the query.
    – Rick James
    Jul 5, 2020 at 18:44
  • @nbk, how would I handle the scenarios where the placeholder is null? Note that this works fine when i remove all the unicode related configs from connection string
    – vigamage
    Jul 6, 2020 at 3:35
  • @RickJames it goes as "inner join table_1 tsc on bt.table_one_id = tsc.id "
    – vigamage
    Jul 6, 2020 at 3:36
  • Show us the complete query, i think you still can#t make :fromDate is null and then replace the place holder, you shoild check the data before you add it to the query
    – nbk
    Jul 6, 2020 at 8:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.