• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving date field from oracle

 
Tim Nachreiner
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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:

 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error do you get?

You can always try (not that it should make any difference)
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • 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.

 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim just saying it doesn't work isn't much help ( http://www.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
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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

 
Martin Vajsar
Sheriff
Pie
Posts: 3751
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic