Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / December 2003

Tip: Looking for answers? Try searching our database.

Trouble getting hours, mins, secs to store in Oracle

Thread view: 
Ed - 11 Dec 2003 13:00 GMT
Folks,

I really hope I am doing something wrong here as it is driving me
nuts.

I have a database table defined as follows:

CREATE TABLE  "Foo" ("Col1" NUMBER(10) NOT NULL, "Col2" VARCHAR2(100)
NOT NULL, "Col3" NUMBER(10) NOT NULL, "StartTime" DATE NOT NULL,
CONSTRAINT "PK_Foo" PRIMARY KEY ("Col1", "Col2", "Col3") );

I am trying to store a "date" field in it which will contain year,
month, day, hour, min and seconds.

I first of all tried to use a java.sql.Date yet quickly moved onto
java.sql.Timestamp

My code is as follows:

       sqlTS = new java.sql.Timestamp(startTime.getTime());
System.out.println("TSvalue is " + sqlTS); // this prints correctly

       // write and commit each row individually
       startStmt.setInt(1,col1);
       startStmt.setString(2,col2);
       startStmt.setInt(3,col3);
       startStmt.setTimestamp(4,sqlTS);
       int rows = startStmt.executeUpdate();
       if (rows != 1)
       {
           throw new Exception("ERROR inserting start row into
database. Expected 1 row but " + rows +
                               " rows were actually inserted");
       }

       conn.commit();

The code runs correctly and sure enough I have a row in the table.

Yet the following SQL:

select to_date("StartTime", 'DD MM YYYY HH24:MI:SS') from "Foo";

returns just:

11-DEC-03

I have tried sql.Date as well with the same results.

I am using Oracle 8.1.7 and the thin jdbc driver on Windows.

Any suggestions would be gratefully received - this is very
frustrating!!

Thanks

Ed
Paul Cooper - 11 Dec 2003 13:25 GMT
>Folks,
>
[quoted text clipped - 53 lines]
>
>Ed

It should be tochar not to_date in your select statement, AFAIR. I am
a bit surprised you aren't getting an error message.

Paul
Andree Große - 11 Dec 2003 14:01 GMT
> ...
> The code runs correctly and sure enough I have a row in the table.
[quoted text clipped - 6 lines]
>
> 11-DEC-03

String sql = "select StartTime from Foo";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
   Object value = rs.getObject(1);
   System.out.println(value.getClass().getName()+": "+value.toString());
}
stmt.close();

And if you use rs.getTimestamp(1) the output format of toString()
will be always: yyyy-mm-dd hh:mm:ss.fffffffff

HTH A.G.
Andree Große - 11 Dec 2003 14:34 GMT
>> ...
>> The code runs correctly and sure enough I have a row in the table.
[quoted text clipped - 6 lines]
>>
>> 11-DEC-03

Another idea:

Don't convert a date in a date...

use: select to_char(StartTime, 'DD.MM.YYYY HH24:MI:SS') from Foo;

Now you can get this value by using rs.getString("StartTime");

Oracle gives you values of date-fields with a default format
of installed database. You can change this for your session
environment or global for database. Please have an look to
Oracle's documentation, how to do that. But if you want to get
special format within a select, you have to do that by hand
like above.

HTH A.G.
Jeff Smith - 15 Dec 2003 13:27 GMT
Like another reply indicates, change:
select to_date("StartTime", 'DD MM YYYY HH24:MI:SS') from "Foo";

to
> select to_CHAR("StartTime", 'DD MM YYYY HH24:MI:SS') from "Foo";

The column does contain all the info you want, you are just formatting it
incorrectly when retrieving.

> Folks,
>
[quoted text clipped - 53 lines]
>
> Ed


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.