Solved

Mapping of Timestamp with Oracle Date datatype

Posted on 2000-04-02
9
726 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
firstChar challenge 13 106
base64 decode encode 12 119
what is the difference between "sudo su" and "su - root" 6 102
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 step-by-step guide to install VisualVM launcher in eclipse.

914 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