Results 1 to 13 of 13

Thread: Error casting

  1. #1
    alxtech is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Java Code:
    utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate")) + " " + df2.format(rs.getTime("EventTime")))); //combines the two fields into date
    Presumably the ClassCast was thrown from here?
    What Date class have you imported, since I notice you fully define java.util.Date and java.sql.Date?

  4. #4
    alxtech is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

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

  5. #5
    alxtech is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

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

  6. #6
    alxtech is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

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

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Also:
    Java Code:
    utilDate = (Date)df4.parse((df3.format(rs.getDate("EventDate" )) + " " + df2.format(rs.getTime("EventTime"))));
    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.
    Finally print out the utilDate.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Oh, and crossposted to OTN.

  12. #12
    alxtech is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    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'

  13. #13
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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

  1. Casting int[][] to int[]
    By subith86 in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 10:48 AM
  2. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 07:59 PM
  3. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 04:31 PM
  4. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-13-2009, 11:07 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •