This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes assigning ResultSet.getDate() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "assigning ResultSet.getDate()" Watch "assigning ResultSet.getDate()" New topic
Author

assigning ResultSet.getDate()

Helen Woods
Greenhorn

Joined: Aug 02, 2001
Posts: 1
I am using ResultSet.getDate() to get a date field from MSSQL Server database. What variable type do I assign the returned value to? getDate() returns a java.sql.Date object. I am having trouble handling this. Have tried casting to String, but you can't do that !!
Any advice would be much appreciated. Thanks.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I'm having trouble with dates too! This is my code (almost feels like a work-around though)

hope this helps,
Jamie

DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34
Hi,
rs is the ResultSet object.Actually
rs.getDate() return the java.sql.Date not java.util.Date.
U have to write like this
java.sql.Date resultDate=rs.getDate(2);
// 2 or 1 according your requirement U write.
Even If U get anyproblem write like this
java.sql.Date reultDate=(java.sql.Date)rs.getDate(2);


Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

according to the documentation, java.sql.Date extends java.util.Date. So casting a java.sql.Date to a java.util.Date should be safe(And it works!). I thought the original question was to convert the java.sql.Date to a String that can be displayed to the user in a readable fashion(since he/she tried to cast it to a String). Unfortunately java.sql.Date is not accepted as a parameter for SimpleDateFormat, thus the cast conversion.
You really can't do much with a java.sql.Date except write back to the database.
Jamie
 
jQuery in Action, 2nd edition
 
subject: assigning ResultSet.getDate()
 
Similar Threads
Time getting truncated in Weblogic
resultset getDate() not retrieving time
How to manipulate the Date object in yyyy-MM-DD HH:MM:SS format.
Time getting truncated in Weblogic
Representing MilliSeconds in JDBC using Oracle