File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Unique primary key generator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Unique primary key generator" Watch "Unique primary key generator" New topic

Unique primary key generator

Dmitry Shekhter

Joined: Feb 21, 2001
Posts: 26
For the project i'm working on, i need to insert data to fill
the tables in the database schema, what i'm trying to figure out is what is an efficient way to generate unique primary keys for each of the tables, as each table has to have a unique primary key, but that data is not going to be provided to my database bean... These primary don't have to be meaningful, they are just to uniquely identify each record and support relationships between the tables in the schema.
I have some ideas, but i was hoping that someone who has already went through this would share the pattern.
Thanks in advance,
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Many databases have database-specific ways to do this. SQL Server can autonumber primary keys. Oracle has sequences to generate numbers. Etcetera.
If you don't want to be database-specific, use some type of service to generate these numbers (can be a class, bean, EJB, RMI or Corba callable service, whatever) in conjunction with a "nextnum" table. For best efficiency, if strictly consecutive numbers are not necessary, you grab a range of numbers from the nextnum table and only go back to the database once you've exhausted that range. With this technique you can have multiple, distributed number generators.
- Peter
I agree. Here's the link:
subject: Unique primary key generator
It's not a secret anymore!