Link to home
Start Free TrialLog in
Avatar of InNoCenT_Ch1ld
InNoCenT_Ch1ld

asked on

Retrieving timestamp from database.

Hi, i have save a timestamp object into my database. then from the ResultSet, i get it as object and store it in a Hashtable.
now, i can only retrieve it as String... how do i convert or parse it back to be object of Timestamp?
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

You can get it from ResultSet like rs.getTimeStamp(int column)
Avatar of CEHJ
DateFormat df = new SimpleDateFormat(XXX);
Date d = df.parse(rs.getString(x);
Timestamp t = new Timestamp(d.getTime());
ASKER CERTIFIED SOLUTION
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cjjclifford
cjjclifford

Don't parse a string returned - this would require the SQL to return a specific format of the date (or your DateFormat to be created with the default string format).

Rather, use getTimestamp, and create a new java.util.Date object from it:

java.util.Date d = new java.util.Date( rs.getTimestamp( x ).getTime() );

Note, you may want to check for null before calling "getTime()"..

Timestamp ts = rs.getTimestamp(x);
java.util.Date d = rs.wasNull() ? null : new java.util.Date( rs.getTime() );
ha... sorry.. you want a Timestamp object.. just use rs.getTimestamp() as StillUnAware suggested...

Note that the SQL has to select a DATE or TIMESTAMP type for this... Or possibly a String column with the correctly formatted date string for JDBC to automatically transpose to the Timestamp value...

>> Don't parse a string returned - this would require the SQL to return a specific format of the date

How do you know the timestamp has not been saved in a custom format as a string?
(getTimestamp will not work if it has). Otherwise why wouldn't it be stored *as* a timestamp?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
InNoCenT_Ch1ld, to avoid speculation, could you post an example of the timestamp?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of InNoCenT_Ch1ld

ASKER

hey guys, sorry for late respond.

to answer the questions here:
1. i create a new timestamp object by taking the date.getTime() as parameter. No format or anything.
    new Timestamp(d.getTime());

2. I used getObject() as i'm going to store it, along with other data, into a Hashtable. Then when i wanted to retrieve it from the Hashtable, i wanted to check if it is a Timestamp object - if yes, then parse it to Timestamp..

*Note: the field is set as datetime in SQL Server, and was used by another application (VB) to store date, eg format: 10/10/2000. Now however, we want it to store the date and the time in that particular field and i reckon Timestamp will be my solution... and I do not know how to convert the 10/10/2000 to Timestamp.
so sorry that i didnt ask the question properly. I actually got error when using this code:

TimeStamp ts = (TimeStamp)hashtable.get(key); //as what  StillUnAware suggested

and i thought that it cannot be done that way.
>> I actually got error when using this code
What error?
some parse error...
i've deleted the code but i think i try to get the time from hashtable as object, then parse it to string..
anyway, now i use what is suggested by StillUnAware


hashtable.put(key, (TimeStamp)rs.getObject(column));

TimeStamp ts = (TimeStamp)hashtable.get(key);

and it clear of those exception. guess the exception is thrown because of the way i handle it...
well, I accept StillUnAware comment (since I've been doing the way he suggested) and split some points among you all for you participation in the stupid question of mine.

tks ;-)
:°)
:-)

It's Timestamp, not TimeStamp
actually i've been wondering for some time... how u make the round object for the smily face, zzynx? ;-)
I think you should really open a new question for an answer to that ;-)
argh, then forget bout it ;D
Only joking IC ! :-)
i know, me too ;-)
guess I might need to post this question in the Lounge.. hehee
you could use a simple cut-n-paste :°)
>> how u make the round object for the smily face, zzynx?
That's the character for degress you find in "20° Celsius"

>> Only joking IC ! :-)
I ***think*** he knows

;°))))
time to go, have a nice day CEHJ
Have a nice day InNoCenT_Ch1ld
>>That's the character for degress you find in "20° Celsius"
hmm, I think i still don't get it, anyway, it's no big deal ;-)

have a nice day, and happy point hunting zzynx and CEHJ (think i saw yours comments all around EE...)
Try Alt + (on number pad) 176
Make that 167
;░)
ASCII 167 = º
ASCII 176 = ¦
At least for me ;°)
Oh OK - your mileage could vary ;-)
ºº Alt + 167
°° Alt + 0176

tks ;-)