How to generate UUID Java and MySQL database data to generate uuid

One, Java

1.UUID Profile

   Meaning UUID is a universally unique identifier (Universally Unique Identifier), which is a software construction standards. Open Source Software Foundation is also applied (Open Software Foundation, OSF) is part of the environmental organization (Distributed Computing Environment, DCE) in the field of distributed computing.

   UUID aim is to make all the elements of a distributed system, can have a unique identification information without having to specify the control terminal identification information to do through the center. In this way, everyone can create a UUID does not conflict with other people. In this case, you do not need to repeat the question when considering the name of the database created.

2. project combat

    UUID as database data table primary key is a very good choice, to ensure that each generated UUID is unique.

    The only flaw is that the results generated UUID string will be longer. UUID standard on this most widely used is Microsoft's GUID (Globals Unique Identifiers).

    UUID standard format: xxxxxxxxxxxx-xxxxxxxx-xxxxxxxxxxxx (8-4-4-4-12).


a. generating a UUID

 public static void main(String[] args) {
            for(int i=0;i<10;i++){
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                System.out.println(uuid);
            }
         
        } 

b. generating a specified number of UUID

 public  static  void main (String [] args) { 
          String U   = getUUID (); 
           . the System OUT .println (U); 
        } 
        / * * 
         * get the specified number of UUID 
         * @param Number UUID obtained the required number int 
         * @return String [] UUID array 
         * / 
        public  static String [] getUUID ( int NUM) {
             IF (NUM < . 1 ) {
                 return  null ; 
            } 
            String [] retArray = new new String [NUM];
             for(int i=0;i<num;i++){
                retArray[i] = getUUID();
            }
            return retArray;
        }

        /**
         * 获得一个UUID 
         * @return String UUID 
         */
        public static String getUUID(){
            String uuid = UUID.randomUUID().toString();
            //去掉“-”符号 
            return uuid.replaceAll("-", "");
        }

Two, MySQL database how to bulk insert data is not repeated uuid

The first step: The data to be queried first listed 

SELECT the UUID (), a.Code, a.name, a.Continent from table 1 a, table B 2 WHERE a.Code = b.CountryCode; 
Note: the UUID () do not split, the split, the same uuid 

second step: inserting data into the table 

insert into table (ID, code, name, Continent) ( 
SELECT the uUID (), a.Code, a.name, a.Continent from table 1 a, table B 2 WHERE a.Code = b.CountryCode 
); 
the third step: updating UUID, the " - " replace 

update TempTable SET ID = SELECT the rEPLACE (ID, ' - ' , '' )

Third, generate UUID 

/ * Only one generation * / 
the SELECT the REPLACE (the UUID (), ' - ' , '' ) the AS ID;
 / * find a plurality of data tables generated plurality of execution in the database * / 
the SELECT (the REPLACE (the UUID () , ' - ' , '' )) the FROM table name ID LIMIT (Article number 50);

Four, sqlServer method of generating a UUID

select ChangE ();

 

Guess you like

Origin www.cnblogs.com/zhaoh-630/p/10959915.html