File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Reading from SQL Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Reading from SQL Server

b rag

Joined: Mar 31, 2005
Posts: 6
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

Joined: May 22, 2003
Posts: 657

Try java.sql.ResultSet.getTimestamp().
Christopher Zinn

Joined: Nov 03, 2003
Posts: 13
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

Joined: Jul 08, 2003
Posts: 24199

Please stop posting new copies of this thread! Continue the discussion here.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Reading from SQL Server
jQuery in Action, 3rd edition