TP_JAVA
TP_JAVA

Reputation: 1002

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.

Upvotes: 0

Views: 1293

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85799

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

Upvotes: 2

Related Questions