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?
now, i can only retrieve it as String... how do i convert or parse it back to be object of Timestamp?
You can get it from ResultSet like rs.getTimeStamp(int column)
DateFormat df = new SimpleDateFormat(XXX);
Date d = df.parse(rs.getString(x);
Timestamp t = new Timestamp(d.getTime());
Date d = df.parse(rs.getString(x);
Timestamp t = new Timestamp(d.getTime());
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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() );
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...
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
InNoCenT_Ch1ld, to avoid speculation, could you post an example of the timestamp?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
so sorry that i didnt ask the question properly. I actually got error when using this code:
TimeStamp ts = (TimeStamp)hashtable.get(k ey); //as what StillUnAware suggested
and i thought that it cannot be done that way.
TimeStamp ts = (TimeStamp)hashtable.get(k
and i thought that it cannot be done that way.
>> I actually got error when using this code
What error?
What error?
ASKER
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(co lumn));
TimeStamp ts = (TimeStamp)hashtable.get(k ey);
and it clear of those exception. guess the exception is thrown because of the way i handle it...
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(co
TimeStamp ts = (TimeStamp)hashtable.get(k
and it clear of those exception. guess the exception is thrown because of the way i handle it...
ASKER
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 ;-)
tks ;-)
:°)
:-)
It's Timestamp, not TimeStamp
It's Timestamp, not TimeStamp
:-)
ASKER
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 ;-)
ASKER
argh, then forget bout it ;D
Only joking IC ! :-)
ASKER
i know, me too ;-)
guess I might need to post this question in the Lounge.. hehee
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
;°))))
That's the character for degress you find in "20° Celsius"
>> Only joking IC ! :-)
I ***think*** he knows
;°))))
ASKER
time to go, have a nice day CEHJ
Have a nice day InNoCenT_Ch1ld
ASKER
>>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...)
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 = ¦
ASCII 176 = ¦
At least for me ;°)
Oh OK - your mileage could vary ;-)
ASKER
ºº Alt + 167
°° Alt + 0176
tks ;-)
°° Alt + 0176
tks ;-)