0

It's written here [MySQL, how to insert null dates ] that MySQL support NULL in datetime field. I am doing this with Java and get next error:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'NULL' for column 'lastvisited' at row 1

If I run in phpmyadmin:

UPDATE `linksbase` SET `lastvisited`=NULL WHERE 1

It is updated.

What is the problemw ith Java code?

prepareStatement("INSERT INTO `linksbase` (`lastvisited`) VALUES ('NULL')");

If I change statement to

prepareStatement("INSERT INTO `linksbase` (`lastvisited`) VALUES (NULL)");

Error is

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'lastvisited' cannot be null
Community
  • 1
  • 1
Tigran
  • 1,049
  • 3
  • 15
  • 31
  • If I run in PHP my admin 'UPDATE `linksbase` SET `lastvisited`=NULL WHERE 1', it is updated. When select result is 0000-00-00 00:00:00 – Tigran Feb 22 '14 at 17:06
  • 1
    Can you show your schema for the `lastvisited` column? Given the last error message, it certainly *looks* like it can't be null... – Jon Skeet Feb 22 '14 at 17:07
  • You was right. It's not null here. – Tigran Feb 22 '14 at 17:08
  • 1
    So how does your PHP work then? Different schema? – Jon Skeet Feb 22 '14 at 17:08
  • Question is closed. Problem was in wrong schema. – Tigran Feb 22 '14 at 17:08
  • @Jon Skeet, No idea. It's not php, but phpmyadmin. May be it automatically set to 0000-000-00, etc. – Tigran Feb 22 '14 at 17:09
  • 3
    Yes, you can activate strict mode of MySQL in which case NULL would be rejected. PMA does not change the configuration so NULL is transformed to 0000-00-00. JDBC foces strict mode, [see this bug](http://bugs.mysql.com/bug.php?id=23371) (but it is indeed a feature!). – Ulrich Thomas Gabor Feb 22 '14 at 17:11

0 Answers0