简体   繁体   中英

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()). But our company only supports mysql, but mysql don't support sequence.

Is there a work around to achieve this using mysql.

MySQL supports auto increment on INTEGER data type. Refer to

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 .

As an example, you can use ID BIGINT PRIMARY KEY AUTO INCREMENT as your field.


If you need this value in your Java application, you can:

  • Generate the id in your MySQL database

  • Get the value in your Java application

  • Use the value to save it in your LDAP

As a test, this will assure that all your elements have 12 digits (based in your last comment):

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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