Search code examples
javamysqlunicodecurrency

how to save currency symbol unicode in mysql table


I'm using mysql table to store currency symbol unicode but when I return it to my android app in dto it showing the same unicode string(\u20B9) and not the currency sign().

Earlier I was using it hard coded like dto.setCurrencyCode("\u20B9") and it was working fine but its not working if I fetch it from mysql table and set in dto.

The mysql table charset is like -

+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field        | Type         | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment |
+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id           | bigint(20)   | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| apiUrl       | varchar(255) | utf8_general_ci | YES  |     | NULL    |                | select,insert,update,references |         |
| countryCode  | varchar(255) | utf8_general_ci | YES  |     | NULL    |                | select,insert,update,references |         |
| currencyCode | varchar(255) | utf8_general_ci | YES  |     | NULL    |                | select,insert,update,references |         |
| locale       | varchar(255) | utf8_general_ci | YES  |     | NULL    |                | select,insert,update,references |         |
| mcc          | int(11)      | NULL            | NO   | MUL | NULL    |                | select,insert,update,references |         |
| msisdnLength | int(11)      | NULL            | NO   |     | NULL    |                | select,insert,update,references |         |
+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+

and table entry is like -

mysql> select * from MccDetails;
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+
| id | apiUrl                              | countryCode | currencyCode | locale | mcc | msisdnLength |
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+
|  1 | https://localhost:8080/api/         | +91         | \u20B9       | en     | 404 |           10 |
|  2 | https://localhost:8080/api/         | +91         | \u20B9       | en     | 405 |           10 |
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+

This is my connection string - jdbc:mysql://127.0.0.1:3306/dbname?autoReconnect=true&useUni‌​code=true&connection‌​Collation=utf8_gener‌​al_ci&characterSetRe‌​sults=utf8

Please let me know what should be the data type of currencyCode column and how to store the unicode value (\u20B9) in table or what is the best way to handle currency symbol in client server architecture.


Solution

  • It is obviously stored as a String not character.

    What you have to do is to enable proper encoding on connection string like that

    jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8"
    

    Strings having chars like ₹ will store correctly to the database when you do that. You dont have to use \u20B9 code anymore