Solved

Mapping of Timestamp with Oracle Date datatype

Posted on 2000-04-02
9
725 Views
Last Modified: 2012-06-27
hi,
 we just have tried to capture the time when user enters the system and this field is stored in the database. here how we do it :

new Timestamp (System.currentTimeMillis ());

this timestamp when we are trying to store in oracle database whose field is of type Date it gives some error as field length is too large.

As timestamp is subclass of date class it should map to date type of oracle datatype.

Kindly help me why is it giving such problems and also is there any way i can store time in milli seconds in oracle database ?

thanks
0
Comment
Question by:pravin_tiwari
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 1

Expert Comment

by:jwilcox
ID: 2678849
I'm guessing the Oralce date class is in the format mm/dd/yyyy?

if so you'll want to store the following method call in your Oracle DB..

DateFormat.getDateInstance(DateFormat.SHORT, Locale.US)

DateFormat is declared in java.text, and getDateInstance is a static member.

If you are not using the miliseconds anywhere other than inside your Java code, why couldn't you just store your miliseconds as a STRING in the DB?
0
 

Author Comment

by:pravin_tiwari
ID: 2678896
hi,
  we have changed the date format of oracle database . point is whether i can map Timestamp class of java to Date datatype of Oracle ? if yes, what all settings are required.
as i apprecaite your idea of storing milli seconds as separate field but right now i will result in lots of client side code changes .

thanks anyway
0
 
LVL 1

Expert Comment

by:technoman
ID: 2678985
Pravin,

 why not try having in your database of type Timestamp?
 The timestamp datatype is of both time and date.

Technoman
0
 
LVL 3

Expert Comment

by:rjackman
ID: 2678998
pravin i think the problem is that u can not assign timestamp directly to the date field in oracle as timestamp contains date aswell as time that is why i think it gives u the error as  field length is too large.
so why dont u use sql.date class  it self
ie.
Date d = new Date(System.currentTimeMillis ());
and then insert it into ur date field
BestLuck
RJackman
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:pravin_tiwari
ID: 2679004
hi rjackman,
    But will it contain the time field also ? becoz this field is my primary key and i want to see to it the it is unique ...now if it has only date field then it might voilate the primary key as it is possible that 2 user will enter my system on same date or may be sam second ! so probability that this field will be same for 2 user if date filed include's milliseconds is rare and that is the reason i want to insert milliseconds fiels also !

thanks anyways
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2679116
java.sql.Date contains onle Date information.

you can use LONGINT database type and save the milliseconds directly.
0
 
LVL 2

Accepted Solution

by:
azawawi earned 100 total points
ID: 2679760
Well this is a weird thing in Java to know that java.sql.Date doesnt store SQL TIMESTAMP type. In fact, java.sql.Date truncates the timestamp part of the DATE. So your only option is to use java.sql.Timestamp instead to correctly update a TIMESTAMP field (or Oracle DATE type-field).

i did that and everything went smoothly. Here is some sample code:

// ...do Oracle driver classs loading...
// ...try to connect to DB...

// get today's timestamp and update the corresponding field in the Database
java.sql.Timestamp today = new java.sql.Timestamp(System.currentTimeMillis());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sql = "UPDATE sometable SET timestamp = to_date(" + tr(df.format(today)) + ",'yyyy-mm-dd hh24:mi:ss')";

// ...free DB resources in reverse order...

for more information on java.sql.Timestamp see http://java.sun.com/products/jdk/1.1/docs/api/java.sql.Timestamp.html

Any more questions dont hesitate.

0
 
LVL 3

Expert Comment

by:rjackman
ID: 2679766
no u can not save time in date field u can refer hey hey's comment then it will work for u..
RJackman
0
 
LVL 2

Expert Comment

by:azawawi
ID: 2679833
you mean add an execute Update and all those little thingies? :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now