• 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

Retrieving date field from oracle

 
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I"m looping a ResultSet named rs and storing the values in variables, mostly strings. How do I store a DATE field? It's not as easy as:

 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error do you get?

You can always try (not that it should make any difference)
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are trying to store the date field into a string, then you need to convert date to string and set the value.

You can always format the string for your own format in the string.

 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim just saying it doesn't work isn't much help ( https://coderanch.com/how-to/java/ItDoesntWorkIsUseless) , as it looks correct to me, so we need to know the exact error ypu atre getting, I know in general the people here are pretty fab, but we aren't miricale workers
 
Tim Nachreiner
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After trapping the error, I could see that the cause was due to referencing an invalid field in the table. Once I used the correct fieldname the error went away. Sorry for the confusion.

I'm still suprised this code worked because I thought the Date object needed to be instantiated somehow, not merely assigned a value. I thought I'd have to use something like

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Nachreiner wrote:After trapping the error, I could see that the cause was due to referencing an invalid field in the table. Once I used the correct fieldname the error went away. Sorry for the confusion.

I'm still suprised this code worked because I thought the Date object needed to be instantiated somehow, not merely assigned a value. I thought I'd have to use something like


There are actually two Date classes, java.util.Date and java.sql.Date. Resultset.getDate() returns the java.sql.Date, but it is a child of java.util.Date and can be therefore used wherever java.util.Date can be. If you want to get rid of the java.sql.Date instance, you can do so the way you've shown (just be careful which version did you actually import), but why should you bother?
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



This won't compile. Java doesn't have a Date( Date ) constructor. You'd have to use
(and hope that your date wasn't null)

I'm still suprised this code worked because I thought the Date object needed to be instantiated somehow, not merely assigned a value.



The date object has been instantiated, you just haven't seen the point where it was done, somewhere deep in the oracle driver code.
It's the same thing as

b is instantiated, it points to the same object as a.What you're doing in your original code sample is equivalent to line 8 of my example.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic