aspose file tools*
The moose likes JDBC and the fly likes getting Date and Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "getting Date and Time" Watch "getting Date and Time" New topic
Author

getting Date and Time

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
I have inserted the date and time to the table like this:
insert into x
values( to_date('11/26/2001:14:00:00', 'mm/dd/yyyy:hh24:mi:ss'));
Then I can retrieve it in SQL
select to_char(date1,'mm/dd/yyyy:hh24:mi:ss') as date1 from x;
but from Jdbc this sql is not working, the resultset is empty.
this is the part of the code:

it prints "Not null" and not next.
Could anyone throw some light on this?
Thanks,
Vanitha.


[This message has been edited by Vanitha Sugumaran (edited November 26, 2001).]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I don't think the code is doing what you intended it to do.
1.
The line "if(rs != null){System.out.println("Not Null");}" accomplishes nothing . The only case in which rs would be null, would be if there was an exception thrown. In that case, the code would just jump to the exception handling block anyways. So just remove this line
2.
This line works fine: "if(!rs.next()){System.out.println("I'm not next");}". It checks to see if there are any results, and moves the cursor to the first row.
3.
This line is not doing what you think: "while(rs.next()){java.sql.Date date1 = rs.getDate("date1");}". In this line you are saying, if there are rows, then process the results...starting at row 2! You never process row 1 because by the time you get to processing your code you have called rs.next() twice, moving you to the second row in your resultset. If there is only one row (and this should be your case), You will get -->it prints "Not null" and not next.
Resolution: something like this:

Does this do what you wanted? If not, what were your intentions?
Jamie

[This message has been edited by Jamie Robertson (edited November 26, 2001).]
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Jamie,
Thanks for your reply. I wasn't clear in my previous message sorry about that. I tried that sql query i didn't get any results so I tried th is null, or not next steps... just to check what's going on..
Ok, now coming to the problem, i tried as you suggested, since the we are retrieving date1 as char, when we getDate("date1"), I get illegal Argument exception.
If I getString("date1"), then it returns the exact stored date value.
Is there any other way to solve this problem? This is my intention, to get the date and time stored in database and compare them to system date. I didn't implement the comparison, just trying to retrieve the values.
Thanks,
Vanitha.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Sorry, I didn't change the sql String. It's hard to retrieve a Date when a String is returned! ...I also used the wrong type of Date! Try this:

let me know how it goes
Jamie
[This message has been edited by Jamie Robertson (edited November 27, 2001).]
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Jamie,
Thanks for your reply. I will check your suggestion and let you know how it works. See, I have solution for my intention, checking whether the time stored in database is equal to system time.
select to_char(date1,'mm-dd-yyyy HH24:MI') from x
where to_char(date1, 'mm-dd-yyyy hh24:mi') = to_char(sysdate,'mm-dd-yyyy hh24:mi');

I am a newbie to Oracle.. so finally I got it. Thanks for your help. I will let you about your code later.
Vanitha.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting Date and Time