Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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 ());}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report