| Author |
Retrieving date field from oracle
|
Tim Nachreiner
Greenhorn
Joined: Apr 28, 2011
Posts: 8
|
|
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
Joined: Jul 21, 2011
Posts: 112
|
|
What error do you get?
You can always try (not that it should make any difference)
|
Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
|
 |
Prabhakar Reddy Bokka
Ranch Hand
Joined: Jul 26, 2005
Posts: 180
|
|
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.
|
SCJP 5, SCWCD 5
|
 |
Wendy Gibbons
Bartender
Joined: Oct 21, 2008
Posts: 774
|
|
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
Joined: Apr 28, 2011
Posts: 8
|
|
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
Bartender
Joined: Aug 22, 2010
Posts: 1219
|
|
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
Joined: Jul 21, 2011
Posts: 112
|
|
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.
|
 |
 |
|
|
subject: Retrieving date field from oracle
|
|
|