Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

Reading from SQL Server

 
b rag
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have a MS SQL Server DB I am reading data from and populating a bean.
I hace a DATETIME Field in the DB and I get a Classcast Exception when I try to read it off my ResultSet. If anybody can share the code of how to get around this, it would be very helpful.

(I never got this problem in Oracle's DATE datatype - rs.getDate(COLUMN_NAME))
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Try java.sql.ResultSet.getTimestamp().
 
Christopher Zinn
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
I've run into various class-cast problems also with dates and timestamps in JDBC.

For me, most of my problems were because I had a java.util.Date on my bean and JDBC returns java.sql.Date (or Timestamp). (I know that java.sql.Date and Timestamp are subclasses of java.util.Date).

I use java.util.Date's only in my beans, and convert to and from Dates and Timestamps as needed when reading/writing to JDBC.

My code, generally looks like this:

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please stop posting new copies of this thread! Continue the discussion here.
 
    Bookmark Topic Watch Topic
  • New Topic