Get the App
SLTechnology News&Howtos  ›  Database  › 

Use Java.util.UUID to generate a unique ID (used as a database primary key)

Shulou Source: shulou.com Published: 2022-06-01 15:27:21 10月23日 Update

UUID (Universally Unique Identifier) globally unique identifiers are numbers generated on one machine that are guaranteed to be unique to all machines in the same space-time. According to the standard developed by the Open Software Foundation (OSF), the Ethernet card address, nanosecond time, chip ID code and many possible numbers are used. By the following combination of parts: the current date and time (the first part of UUID is related to time, if you generate a UUID and then generate a UUID a few seconds later, then the first part is different, the rest is the same), clock sequence, globally unique IEEE machine identification number (if there is a network card, obtained from the network card, no network card is otherwise obtained), the only drawback of UUID is that the resulting string will be longer.

There are several main ways to generate UUID in Java:

JDK1.5

If you use JDK1.5, it becomes easy to generate UUID, thinking that JDK implements UUID:

Java.util.UUID, just call it directly.

UUID uuid = UUID.randomUUID ()

String s = UUID.randomUUID (). ToString (); / / the primary key id used to generate the database is very good.

UUID is made up of a sixteen-digit number in the form of, for example,

550E8400-E29B-11D4-A716-446655440000

/ / the following is the utility class that implements the acquisition of a unique primary key id for the database

Package com.cn.util;import java.util.UUID;public class UuidUtil {public static String get32UUID () {String uuid = UUID.randomUUID (). ToString (). Trim (). ReplaceAll ("-", "); return uuid;} public static void main (String [] args) {System.out.println (get32UUID ());}}

Tags: Generation number time machine network card part data database global way different good same address fund foundation that is tool sequence form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno macOS Shulou Information Microsoft