• Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 1305
36
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: 1305
36
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: 1818
238
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: 1818
238
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: 23451
46
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.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!