• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to store date from java program in MySql database having table with column of DATETIME datatype?

 
Greenhorn
Posts: 4
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have table with two columns. First one's datatype is String and Second one's datetype is DATETIME.
How to store data in second column.

My code is as follows:


It is giving me an error as follows:
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you are using an incorrect format for the date time/date time stamp.

https://dev.mysql.com/doc/refman/5.7/en/datetime.html wrote:The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format.
The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.


You can find out more here https://dev.mysql.com/doc/refman/5.7/en/datetime.html
However that is simply the default and can be changed on the server.

You may want to check out https://dev.mysql.com/doc/refman/5.7/en/date-and-time-literals.html to see what other options you can use.

Really all that you need to do is replace this yyyy/MM/dd with this yyyy-MM-dd.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to CodeRanch Pritam.

I hope that you find the community welcoming and helpful.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The date parameter in the PreparedStatement.html#setDate method is a java.sql.Date type, not a java.util.Date type.
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:The date parameter in the PreparedStatement.html#setDate method is a java.sql.Date type, not a java.util.Date type.


Your compiler should have reported something like this:
The method setDate(int, java.sql.Date) in the type PreparedStatement is not applicable for the arguments (int, java.util.Date)
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the bits needed to get "now" into a JDBC date-time field:







No need to mess around with formatting timestamps as Strings. Also, if your column type is DATETIME then you need setTimestamp(); setDate() will only set the date part of the column. (The designers of Java were not quite at the top of their game when they used "Date" as the name of a class to encapsulate date and time information, so generations of beginners have been confused.)
reply
    Bookmark Topic Watch Topic
  • New Topic