Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to convert and insert date into mysql

 
Pranit Sonawane
Ranch Hand
Posts: 172
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I searched many forums and finally all I found is this



but its still showing an exception:- java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

Can anybody please help!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DateFormat.parse() returns a Date, so why are you trying to cast it to what it already is?

Getting rid of superfluous code is a good first step when diagnosing a problem.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a java.util.Date object and you want to convert it to a java.sql.Date object, casting isn't the way to do that. (That's because java.util.Date isn't a subclass of java.sql.Date.) To do the conversion, create a new java.sql.Date object from the java.util.Date object -- check out the available constructors in the API documentation.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what is occurring on line 8 and 9.

It's not clear where the exception is coming from, but I suspect cleaning up the code will make it easier to find.

In the future, please be sure to include the line number of the statement that is causing the exception.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error messages are complaining about casts to java.sql.Date. There are casts to "Date" in the code. A natural inference would be that "import java.sql.Date" was in the part of the code not posted. It wouldn't be hard for somebody with access to all of the code to look and see.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're a step a ahead. I'm assuming that when the unnecessary casts are removed, that that's what will be revealed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic