This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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:
 
Bartender
Posts: 1868
81
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
Bartender
Posts: 1868
81
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.
 
Saloon Keeper
Posts: 2351
293
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
Saloon Keeper
Posts: 2351
293
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: 24086
54
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.)
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!