java - 如何在mysql表中保存货币符号unicode

标签 java mysql unicode currency

我正在使用 mysql 表来存储货币符号 unicode 但是当我将它返回到我的 dto 中的 android 应用程序时,它显示相同的 unicode 字符串(\u20B9)而不是货币符号().

早些时候,我使用它进行硬编码,如 dto.setCurrencyCode("\u20B9"),它工作正常,但如果我从 mysql 表中获取它并在 dto 中设置,它就无法工作。

mysql 表字符集是这样的-

+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| 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 |         |
+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+

表格条目就像-

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 |
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+

这是我的连接字符串 - jdbc:mysql://127.0.0.1:3306/dbname?autoReconnect=true&useUni‌ code=true&connection‌ Collat​​ion=utf8_gener‌ al_ci&characterSetRe‌ sults=utf8

请告诉我 currencyCode 列的数据类型应该是什么以及如何在表中存储 unicode 值 (\u20B9) 或者在客户端服务器架构中处理货币符号的最佳方式是什么.

最佳答案

它显然存储为字符串而不是字符。

你所要做的就是像那样对连接字符串启用正确的编码

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

当您执行此操作时,具有 ₹ 等字符的字符串将正确存储到数据库中。您不必再使用\u20B9 代码

关于java - 如何在mysql表中保存货币符号unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46482612/

相关文章:

mysql - 相同的 MySQL 查询在 5.6 中的运行速度比在 5.1 中慢得多

c++ - 在 C++ win32 控制台应用程序中输出 unicode 符号 π 和 ≈

java - 用 ASCII 近似值替换 unicode 标点符号

mysql - SQL数据约束疑虑

mysql quarter() 函数返回 Jan-Mar 作为第一季度。 4 月至 6 月在印度第一季度

python - 在 python 2.7 中处理非英文文本

java - Selenium - webDriver 不稳定错误堆栈跟踪

java - 多个 toString() 用于 listview<object> 适配器

java - 等待元素的可见性

java - PermGen 空间 - maven m2e tomcat7