[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Mapping of Timestamp with Oracle Date datatype

Posted on 2000-04-02
9
Medium Priority
?
734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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 300 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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