Wednesday, April 20, 2011

uniqueidentifier in Sql Server database - how to generate in Java environment?

I have a database table that uses Sql Server 2005's uniqueidentifier column.

I know how to create GUIDs in C#...

string newid = System.Guid.NewGuid().ToString();

However, the application that is inserting into this database is running Java.

Is there a way to generate Sql-Server-compatible GUIDs using Java code?

(NOTE: generating the GUIDs on the Sql Server side is not an option in this case)

Is this - - equivalent to the C# version?

From stackoverflow
  • The code you linked ought to work fine. For kicks, here's another implementation with some good documentation to boot.

    GUID's are a pretty well defined phenomenon, typically involving a unique machine identifier and the current time and then a bunch of randomness. Anybody who has taken the time to write a GUID library ought to have spent enough time on it to be much better at it than you or I are, so using a library is the right choice. The code I linked has a bunch of links to the IETF specs etc, so I think that's a pretty good sign that they did their due diligence.


  • Why not simply use the built in class java.util.UUID, assuming you are using at least Java 1.5


Post a Comment