Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2004

Tip: Looking for answers? Try searching our database.

Duplicate key or integrity constraint violation - Mysql

Thread view: 
FreudianSlip - 17 Jun 2004 10:50 GMT
Hi all,

I have recently started programming in Java, so forgive me if theres
something very basic about the problem i'm having.

I have MySQL 4.0.15 running on linux box quite happily and am using
Java 1.4.2 and Netbeans 3.6 to access and update the database.

One table has an autoincrement field (counter) and i get the following
message when I try to insert a record in the table.

Duplicate key or integrity constraint violation,  message from
server: "Duplicate entry 'null' for key 1"

I have tried various ways to perform the insert, using:

String SQL = "insert into Header values (NULL,'" + getClusterID() +
"','" .....

and

String SQL = "insert into Header values (0, '" + getClusterID() +
"','" .....

along with:

String SQL = "insert into Header (ClusterID,TStamp ...etc ) values
(getClusterID() + "','" .....

But still I get the error.

If I check the table after the program fails, the record has been
successfully inserted with the correct auto increment figure, however
of course the program stops dead at that point and does not continue.

Any help would be much appreciated.

Thanks

W
Colin 't Hart - 17 Jun 2004 19:47 GMT
<snip>

> String SQL = "insert into Header values (NULL,'" + getClusterID() + "','"

<snip>

> String SQL = "insert into Header values (0, '" + getClusterID() + "','"

This way of inserting is not recommended! If someone was to add columns
to the table or somehow the order of the columns was changed, then your
application would immediately cease to work. It has more chance to
continue working (= more robust = a quality of good software) if you
specify which columns you wish to insert into. Of course if you then
desire to add columns later, you can only do so without requiring the
code also be updated if the columns allow NULLs (avoid this if possible)
or specify a default value for the new column.

<snip>

> String SQL = "insert into Header (ClusterID,TStamp ...etc ) values
> (getClusterID() + "','" .....

This way is recommended!

What's the name of the column with the autoincrement field? You probably
shouldn't specify this in your insert.

But it looks like you aren't specifying it in your last alternative.

Sorry I can't help more.

Cheers,

Colin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.