Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting date from oracle/other databases exactly to java

 
ch praveen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

Iam creating a simple program which extracts data from any database and stores it in an XML file and also it can extract data from an XML file and updates the database.

My program is just getting the tablename as an input and from that it builds the query (select * from tablename). Through ResultSet, Iam extracting date fields with getDate() method of ResultSet, which is yeilding converted dates [In oracle if date is 23-DEC-1980, Im getting it as 1980-12-23 in java]. Even getString() method of ResultSet doesn't work.
I want my program to work with any database engine, which may have their own date formats. Kindly can anyone suggest me a way to extract date from any database exactly as it is in java.


NOTE:: I can't use to_char(datefield) type commands in SQLQUERY as the only input to my program is tablename and my query is, select * from tablename.

Regards,
Ch.Praveen.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the database you get a Java Date object (or Calendar or one of those.) The toString() method has a default format which you are seeing. You can use a DateFormat object to tell it how you'd like toString() to format. DateFormat can also parse a string of matching format into a Date object to go the other way. See if this helps!

BTW: You might want a DateFormat with no timezone adjustment. We had some code that pulled timestamps out of the database and when daylight savings time kicked in (or out) it started adding an hour to the time!
 
ch praveen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Thanks for your quick reply. Can you kindly give an example for your answer.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JDBC API is meant to work for different kinds of database. Regardless of how your date is actually stored in databases such as Oracle, MySQL or MS-SQL, you're able to get it as a Java object, i.e., java.util.Date. It really doesn't matter that what you're getting is different in terms of format.

By using the SimpleDateFormat class, you could format the date into any format that you need. So don't worry, you're already on the right track.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic