简体   繁体   English

在Java应用程序中生成12位唯一编号

[英]Generate 12 digits unique number in java application

I've to store user profiles in LDAP with 12 digits unique ID, so thought of using database by creating a sequence and accessing it in Java code(seq.nextVal()). 我必须使用12位唯一ID将用户配置文件存储在LDAP中,因此考虑通过创建序列并以Java代码(seq.nextVal())访问它来使用数据库。 But our company only supports mysql, but mysql don't support sequence. 但是我们公司仅支持mysql,但mysql不支持序列。

Is there a work around to achieve this using mysql. 是否可以使用mysql来解决此问题。

MySQL supports auto increment on INTEGER data type. MySQL支持对INTEGER数据类型进行自动递增。 Refer to 参考

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

If you want to make the field UNIQUE , then you can set it as PRIMARY KEY . 如果要使字段UNIQUE ,则可以将其设置为PRIMARY KEY

As an example, you can use ID BIGINT PRIMARY KEY AUTO INCREMENT as your field. 例如,您可以使用ID BIGINT PRIMARY KEY AUTO INCREMENT作为字段。


If you need this value in your Java application, you can: 如果您在Java应用程序中需要此值,则可以:

  • Generate the id in your MySQL database 在您的MySQL数据库中生成ID

  • Get the value in your Java application 在Java应用程序中获取价值

  • Use the value to save it in your LDAP 使用该值将其保存在LDAP中

As a test, this will assure that all your elements have 12 digits (based in your last comment): 作为测试,这将确保您的所有元素都具有12位数字(基于您的上一条评论):

CREATE TABLE uniqueids(id BIGINT PRIMARY KEY AUTO_INCREMENT);

INSERT INTO uniqueids VALUES(100000000000);

INSERT INTO uniqueids VALUES(null);

SELECT *
FROM uniqueids

Tested on MySQL 5.0 在MySQL 5.0上测试

SQL Fiddle: http://www.sqlfiddle.com/#!2/8c508f/1 SQL小提琴: http ://www.sqlfiddle.com/#!2 / 8c508f / 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM