This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Greenhorn
Posts: 3
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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:
 
Saloon Keeper
Posts: 1844
80
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 1844
80
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to CodeRanch Pritam.

I hope that you find the community welcoming and helpful.
 
Bartender
Posts: 2038
273
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2038
273
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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)
 
Sheriff
Posts: 23706
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!