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