Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] Auto Number?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 JensB   Members   -  Reputation: 189

Like
Likes
Like

Posted 11 June 2001 - 02:04 AM

I am writing a Java application that uses JDBC to work with a database - The idea is that the app should be able to access any database engine (for instance MySQL). Therefore I don''t wanna use stored procedures, but then a problem arises - What about Counter / AutoNumber type of fileds: I insert a row into the db, the db engine will give it a unique number - How do I get this number back without using a stored procedure - Is there any good way, or shall I skip using the "Counter" functionallity of the database and do the numbering myself? any input appreciated, Jens

Sponsor:

#2 Neurotic   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 June 2001 - 03:11 PM

Jens,

The standard way of providing primary Ids is to create them yourself, rather than allow the dB to do it.

The easiest way to do it is the following:

ID = day + month + hour + second + minute + randRange(0,100) + randRange(0,100);

(althought thats some strange pseudo code, and it reality, yoiu can place them in any order you like..or randomize the order? for extra security.)

But it will provide you with a 99.9999999...etc percent sure way of generating a unique ID (hasn''t failed me yet)

The added benefit is that you know the ID when you create the item, so you can use it as you see fit elsewhere.

Mark

----
Vector7 Software Engineering
www.vector7.com.au
----

#3 JensB   Members   -  Reputation: 189

Like
Likes
Like

Posted 14 June 2001 - 02:57 AM

MArk,

This method failed me immediatly, but that happened when I generated a bunch of ids very quickly.

Any ideas on how to fix that (adding milliseconds to the equation didn''t help).

Jens

#4 Neophyte   Members   -  Reputation: 543

Like
Likes
Like

Posted 14 June 2001 - 04:53 AM

If the database you''re using supports sequences (I''ve only used Oracle, and I don''t know if this is a regular relational database feature or Oracle-specific), you can do this:

  
// Assuming Statement stmt, and a sequence in the

// database named myseq

ResultSet rs;
rs = stmt.executeQuery ("select myseq.nextval from dual");
int id = rs.getInt(1);
// rs.close etc.



Obviously you would have to sprinkle the requisite try/catch-statements in there as well.

Neophyte

- Death awaits you all with nasty, big, pointy teeth. -




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS