This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "assigning ResultSet.getDate()" Watch "assigning ResultSet.getDate()" New topic

assigning ResultSet.getDate()

Helen Woods

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,

Ranch Hand

Joined: Aug 06, 2001
Posts: 34
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.
I agree. Here's the link:
subject: assigning ResultSet.getDate()
It's not a secret anymore!