1

I want to pass sys date to my procedure. Below is my code snippet

 DateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");
                                           Date date = new Date();
String insertquery="{ call sp_process_job (?,?,?) }";

                    cs = con.prepareCall(insertquery.toString());
                    cs.setString(1,id);
                    cs.setString(2,host);
                    cs.setDate(19,(java.sql.Date) date);
                                          cs.execute();
                          con.commit();

My stored Procedure

create procedure sp_process_job (@request_id varchar(25),@host varchar(20),@created_on varchar(25)) as

begin

set dateformat mdy
SELECT CAST(@created_on as datetime)
insert into t_job_details(request_id,host,created_on,run_date) 
                values(@request_id,@host,@created_on)

end

getting this error

java.util.Date cannot be cast to java.sql.Date.

Sirko
  • 72,589
  • 19
  • 149
  • 183
Edward
  • 1,367
  • 8
  • 26
  • 43
  • Just import java.util.*; – Lucifer Jun 04 '12 at 04:24
  • did the same...them I am getting error on " Date date = new Date();" this line – Edward Jun 04 '12 at 04:29
  • Not directly related, but note that prefixing your SQL Server SPROCS with sp_ isn't regarded as good practice - http://stackoverflow.com/questions/238267/what-is-your-naming-convention-for-stored-procedures and http://stackoverflow.com/questions/871612/whats-the-best-practice-of-naming-stored-procedure-for-t-sql – StuartLC Jun 04 '12 at 05:25

1 Answers1

1

You can't cast java.util.Date to java.sql.Date because the second is a subclass of the first.

You could do:

cs.setDate(19, new java.sql.Date(date.getTime()));

And remember that java.sql.Date will have everything but the month, day, and year zeroed out. As the java.sql.Date javadoc says:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66