• 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

Trying to convert and insert date into mysql

 
Ranch Hand
Posts: 172
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic