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
Solved

Mapping of Timestamp with Oracle Date datatype

Posted on 2000-04-02
9
728 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

860 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