哪个是MySQL中电话号码的最佳数据类型,它的Java类型映射应该是什么? [英] Which is best data type for phone number in MySQL and what should Java type mapping for it be?
问题描述
我在我的Web应用程序中使用带有Spring JDBC模板的MySQL。我需要存储只有数字(10)的电话号码。我对使用数据类型的数据类型感到有点困惑。
I am using MySQL with Spring JDBC template for my web application. I need to store phone number with only digits (10). I am little bit confused about data type using data type.
- MySQL的首选数据类型是什么?
- Bean(POJO)类中的Java数据类型应该是什么?
- 如何使用javax验证/约束来验证该数据类型的长度,并且只允许数字?
推荐答案
Strings& VARCHAR。
Strings & VARCHAR.
-
请勿尝试将电话号码存储为实际号码。它会破坏格式,删除前面的
0
和其他不良内容。
你可以,如果您选择,将用户输入限制为仅数值,但即使在这种情况下,请将支持持久数据保留为字符/字符串而不是数字。
You may, if you choose to, restrict user inputs to just numeric values but even in that case, keep your backing persisted data as characters/strings and not numbers.
请注意在尝试实施任何类型的长度限制,验证或掩码(例如XXX-XXXX-XX)之前,更广泛的世界以及它们的数字长度和格式如何不同。
Be aware of the wider world and how their number lengths and formatting differ before you try to implement any sort of length restrictions, validations or masks (eg XXX-XXXX-XX).
非数字字符在电话号码中有效。一个典型的例子是 +
作为国际号码开头的 00
的替代。
Non numeric characters can be valid in phone numbers. A prime example being +
as a replacement for 00
at the start of an international number.
在评论中的对话中编辑:
Edited in from conversation in comments:
- 这是电话号码与数字有关的更大的UI错误之一。考虑和对待它们就像地址一样好得多,它比实际的更接近并代表电话数字。
这篇关于哪个是MySQL中电话号码的最佳数据类型,它的Java类型映射应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!