0

I have a JDateChooser in my form. and I need to insert it's Date value into DB. I used this method just after "public class Nonacademic extends javax.swing.JInternalFrame {" ,

and the method I used is mentioned below,

public static java.sql.Date convertUtilDateToSqlDate(java.util.Date date){

                if(date != null) {
                java.sql.Date sqlDate = new java.sql.Date(date.getTime());
                    return sqlDate;
                    }
                JOptionPane.showMessageDialog(null, "No Dates are      Specified!");
                return null;
                }

and In my Add button's actionPerformed event I used

             Connection c=DBconnect.connect();
             Statement s = (Statement) c.createStatement();
             PreparedStatement statement =  c.prepareStatement("INSERT into nonacademic ( empId, name, Dob, JoinedDate) VALUES (?,?,?,?)");

           statement.setString(1,txtEmpId.getText());
           statement.setString(2, txtNmae.getText());
           statement.setDate(3,convertUtilDateToSqlDate( (Date)     jDateChooserDOB.getDate()));
           statement.setDate(4, convertUtilDateToSqlDate( (Date) jDateChooserDateOfJoined.getDate()));

statement.executeUpdate();

Problem is It is gives this error, java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

When I search for a solution to this, I found that this runtime error happens due to Parent class Instance is casting into child class.So can u give me a suggestion to correct this code.

Note: After done coding above code when I select a Date in JDateChooser It appears as this 2015-08-06, Before code above stuff It appears as Aug 6,2015.

  • Have you tried getting rid of `(Date)` in your `statement.setDate` calls – MadProgrammer Aug 19 '15 at 03:56
  • when i type statement.setDate(3,convertUtilDateToSqlDate( jDateChooserDOB.getDate())); (without (Date) it' ll show a error and display a message saying, to cast it. –  Aug 19 '15 at 04:45
  • Then, `jDateChooserDOB.getDate()` probably isn't returning `java.util.Date`, at a guess... – MadProgrammer Aug 19 '15 at 04:47

1 Answers1

0

Try below statements,

statement.setDate(3,convertUtilDateToSqlDate(jDateChooserDOB.getDate()));
statement.setDate(4, convertUtilDateToSqlDate(jDateChooserDateOfJoined.getDate()));

Reason: This is because of import statements. You might have imported only java.sql.Date or java.sql.* statement in your code. All "Date" class you mentioned in your program will be treated as java.sql.Date. So JVM is trying to converting java.util.Date to java.sql.Date in those statement and throwing exception.

Venkatesh Goud
  • 616
  • 2
  • 6
  • 22
  • I import java.util.Date in my code as u have mentioned here I have only import java.sql.Date Then I removed (Date) from statement.setDate. My problem was solved but I'm still confused one thing that is From importing java.util.Date How is it casting a parent instance(java.util.date) into it's child instance(java.sql.date) can someone give me a clear explanation Please, It will be useful for me to improve my java Knowledge :-) :-) –  Aug 19 '15 at 04:57
  • I import java.util.Date in my code. and as u have mentioned here, I have only imported java.sql.Date Then I removed (Date) from statement.setDate. My problem was solved but I'm still confused about one thing that is From importing java.util.Date How is it casting a parent instance(java.util.date) into it's child instance(java.sql.date) can someone give me a clear explanation Please, It will be useful for me to improve my java Knowledge :-) :-) –  Aug 19 '15 at 05:03