aspose file tools
The moose likes JDBC and the fly likes Extracting date from oracle/other databases exactly to java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Extracting date from oracle/other databases exactly to java" Watch "Extracting date from oracle/other databases exactly to java" New topic
Author

Extracting date from oracle/other databases exactly to java

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
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

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hi James,

Thanks for your quick reply. Can you kindly give an example for your answer.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to JDBC...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Extracting date from oracle/other databases exactly to java
 
Similar Threads
JDBCSAXParser
problem in query using java code
retriving bit from Database
Is there a way of storing a resultset in a variable (not copying into arrays etc.)
problem in query using java code