Results 1 to 13 of 13
Thread: Error casting
- 05-26-2011, 03:44 PM #1
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
Error casting
Hello Forum,
I am retrieving records from myslq database to oracle database,
in mysql, i have a date field and a time field for each record, in oracle i have a timestamp field, what i am doing is extracting the two mysql fields, then parsing to a java.util.Date and finally passing that to the destination database as a java.sql.date type. currently i am getting a java.lang.ClassCastException: java.util.Date
this is the code:
java.util.Date utilDate = new java.util.Date();
utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate" )) + " " + df2.format(rs.getTime("EventTime")))); //combines the two fields into date
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); // cast to sql,Date
destStmt.setDate(EDATE, sqlDate); // pass value to prepared statement
- 05-26-2011, 04:02 PM #2
Try debugging it by printing out the object you are trying to cast to see what info the output contains. Sometimes it will show you what class it is.
- 05-26-2011, 04:57 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Presumably the ClassCast was thrown from here?Java Code:utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate")) + " " + df2.format(rs.getTime("EventTime")))); //combines the two fields into date
What Date class have you imported, since I notice you fully define java.util.Date and java.sql.Date?
- 05-26-2011, 05:27 PM #4
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
the canversion from java.util.Date Object to a java.sql.Date is working but on the final java.sql.Date the time is been replace by 12:00:00 AM
is there a way to preserve the original time from java.util.Date Object:
prior conversion:
java.util.Date Object = 21-Jun-2011 3:30:00 PM
java.sql.Date Object = 21-Jun-2011 12:00:00 AM
java.util.Date utilDate = new java.util.Date();
utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate" )) + " " + df2.format(rs.getTime("EventTime"))));
System.out.println("this is utilDate dateType '" + (df3.format(rs.getDate("EventDate")) + " " + df2.format(rs.getTime("EventTime"))) + "' ");
java.sql.Date finalDate = new java.sql.Date(utilDate.getTime());
System.out.println("this is the '" + finalDate + "' ");
destStmt.setDate(EDATE, finalDate);
- 05-26-2011, 06:24 PM #5
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
the conversion from a java.util.Date Object to a java.sql.Date Object
works but the time in the new java.sql.date finalDate is lost, it goes to 12:00:00 AM
//java.util.Date Object = 14-May-2011 3:00:00 PM
//java.sql.Date Object = 14-May-2011 12:00:00 AM
is there a way to preserve same time in the new java.sql.Date object?
java.util.Date utilDate = new java.util.Date();
utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate" )) + " " + df2.format(rs.getTime("EventTime"))));
System.out.println("this is utilDate dateType '" + (df3.format(rs.getDate("EventDate")) + " " + df2.format(rs.getTime("EventTime"))) + "' ");
java.sql.Date finalDate = new java.sql.Date(utilDate.getTime());
System.out.println("this is the '" + finalDate + "' ");
destStmt.setDate(EDATE, finalDate);
- 05-26-2011, 07:17 PM #6
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
i changed type to java.sql.Timestamp but still same error. date gets to destination database with time 12:00:00 AM
java.util.Date utilDate = new java.util.Date();
utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate" )) + " " + df2.format(rs.getTime("EventTime"))));
System.out.println("this is utilDate dateType '" + (df3.format(rs.getDate("EventDate")) + " " + df2.format(rs.getTime("EventTime"))) + "' ");
java.sql.Timestamp finalDate = new java.sql.Timestamp(utilDate.getTime());
System.out.println("this is the timestamp: '" + finalDate + "' ");
destStmt.setTimestamp(EDATE, finalDate);
- 05-26-2011, 08:54 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Tolls asked what Date class you have imported. If you answered his question, he might be able to help you.
Please post the full error message text and show the code line it refers to.
What kind of Date class does the df4.parse(..) method return?
- 05-26-2011, 09:07 PM #8
Frustrating when your suggestions are ignored. If the OP had done what was suggested in post #2 that might show the class that was returned by df4.parse(). If the object's toString was called, then calling .getClass() might show the class.
- 05-27-2011, 10:11 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Hang on.
Your OP said you were getting a ClassCastException, yet now it's something different?
Have you solved the original problem then?
If so, can you say so (and what the problem was) so the rest of us can stop chasing the wrong problem.
- 05-27-2011, 10:16 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Also:
show us what df2, df3 and df4 are, and what the values returned but the getDate and getTime calls (print out the resulting date objects), and the result of the formatting of them.Java Code:utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate" )) + " " + df2.format(rs.getTime("EventTime"))));
Finally print out the utilDate.
- 05-27-2011, 10:44 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Oh, and crossposted to OTN.
- 05-27-2011, 04:15 PM #12
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
I am combining both source fields now in a calendar object, then i am creating a date object to convert then to an sql.timestamp which is the final value to pass to prepared statement
see comments in code, i am not getting any type of exception the problem is that the final output to the oracle database is wrong in the time part, event though the sqlDate object has
a value 2011-05-28 10:30.00.0 the time is lost in the database and it is replace by 28-May-2011 12:00:00 AM
This is my code:
Calendar dCal = Calendar.getInstance(); //Two calendar objects created to replace combine date and time from source database
dCal.setTime(rs.getDate("EventDate"));
Calendar tCal = Calendar.getInstance();
tCal.setTime(rs.getTime("EventTime"));
dCal.set(Calendar.HOUR_OF_DAY, tCal.get(Calendar.HOUR_OF_DAY));
dCal.set(Calendar.MINUTE, tCal.get(Calendar.MINUTE));
dCal.set(Calendar.SECOND, tCal.get(Calendar.SECOND));
dCal.set(Calendar.MILLISECOND, tCal.get(Calendar.MILLISECOND));
Date finalDate = dCal.getTime(); //util.Date object contains the calendar date
System.out.println("this is the date object finalDate: '" + finalDate + "' ");
java.sql.Timestamp sqlDate = new java.sql.Timestamp(finalDate.getTime()); //sql.Timestamp object because sqlPrepared statement if of the type java.sql object
System.out.println("this is the sql.Timestamp sqlDate: '" + sqlDate + "' ");
destStmt.setTimestamp(EDATE, sqlDate);
This is the output of the objects:
string combine date and time from source: '28-May-2011 10:30:00 AM'
utilDate Previous string converted to util.Date: 'Sat May 28 10:30:00 EDT 2011'
this is the date object finalDate: 'Sat May 28 10:30:00 EDT 2011'
this is the sql.Timestamp sqlDate: '2011-05-28 10:30:00.0'
- 05-27-2011, 10:47 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
I'm confused - the OP wants help with a class cast exception, but all questions and suggestions about it are ignored. Instead we get a whole bunch of code snippets without any explanation, and still all questions and suggestions are ignored.
It reads like some train-of-thought debugging blog...
I have nothing further to contribute.
Similar Threads
-
Casting int[][] to int[]
By subith86 in forum New To JavaReplies: 2Last Post: 02-02-2011, 10:48 AM -
Casting
By zzpprk in forum Advanced JavaReplies: 13Last Post: 08-13-2009, 07:59 PM -
What does casting mean?
By sev51 in forum New To JavaReplies: 3Last Post: 01-27-2009, 04:31 PM -
casting help
By soc86 in forum New To JavaReplies: 4Last Post: 01-13-2009, 11:07 PM -
Casting
By leebee in forum New To JavaReplies: 5Last Post: 08-10-2007, 12:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks