Custom table ID generated by java and id generated by java

Source: Internet
Author: User

Custom table ID generated by java and id generated by java

The following ID must be generated:

56d7ade1-87d1-4f54-8dc8-13611c8c2545
27181ad4-4214-4e12-af3a-911a0103a12f
24cafdfb-eac3-4567-80c0-70d21d096b19
C92046a0-3d94-4d72-ba8f-cdac30ed69e8
1e607dde-eef6-49ac-a3b1-16ba1475d293


In java, you can use the following code:

UUID uuid = UUID. randomUUID ();


System. out. println (uuid. toString ());


How does JAVA code generate an ID based on the ID rules in the table? A similar piece of code

Set automatic growth in SQL Server. The step size is 1.
Do not insert the ID value when inserting.
For example:
The my_table table has fields: id (primary key auto-increment), title, author, content
Insert String SQL = "insert into my_table values ('" + title + "', '" + author + "', '" + content + "')";
========================================================
Or you can use a sequence to achieve automatic growth:
Sequence
Create sequence my_id_seq
Increment by 1
Start with 1
Max value 999999999
NOCYCLE;
Use my_id_seq.nextval when adding
For example:
String SQL = "insert into my_table
Values (my_id_seq.nextval, '"+ title +"', '"+ author +"', '"+ content + "')";

Java method for automatic ID generation

Oracle:
Create sequence name
Create sequence XX_seq;
Auto-growth: XX_seq.nextval
MySql:
Add auto_increment after the primary key when creating the table. For example:
Create table student (
Id int (4) primary key auto_increment,
Name varchar (30) not null
);
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.