简体   繁体   中英

how to insert current date in datetime field in mysql using java

I am working on date time using Java. I am trying to insert the current date in MySQL. The data type for the table in database is datetime. When I am trying to insert the data, an exception is thrown

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

I want to insert current date in in that field.

It is recommended that you insert the current date using SQL rather than Java. To do so, use the MySQL CURDATE() function. Your query will look like this:

INSERT INTO orders (order_date, order_description)
     VALUES (CURDATE(), 'Jarlsberg Cheese');

Note that CURDATE() gives you the date only. If you want the time you need to use NOW() , or CURTIME() :

  • NOW() : Gives you the date and time, eg 2014-11-11 12:45:34.
  • CURDATE() : Gives you the date only, eg 2014-11-11.
  • CURTIME() : Gives you the time only, eg 12:45:34.

You can use NOW() function of mysql. NOW() returns the current date and time.

Hope it will help you. Happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM