Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning ResultSet.getDate()

 
Helen Woods
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic