1

I have a Stored Procedure that requires the following input parameters -

PROCEDURE `Report_Publishing`(p_StartDate datetime, p_EndDate dateTime, p_Action varchar(15))


From the Frontend I am getting the start and end dates in following format -
start as a String "2017-03-15" and end as a String "2017-04-17",

Following is the piece of code, to convert the start and end dates into the Stored Procedure's required format -

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.text.SimpleDateFormat; 

public List<PublishReportDto> getExport(String start, String end, String action, Connection conn, String sql) {
        List<PublishReportDto> publishList = null;

        try {
            CallableStatement cs = conn.prepareCall(sql);

            start += " 00:00:00";
            end += " 00:00:00";
            Date startDate = null;
            Date endDate = null;
            try {
                startDate = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(start);
                endDate = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(end);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            cs.setDate(1,(java.sql.Date) startDate);
            cs.setDate(2,(java.sql.Date) endDate);
            cs.setString(3, action);
            cs.registerOutParameter(1, java.sql.Types.DATE);
            cs.registerOutParameter(2, java.sql.Types.DATE);
            cs.registerOutParameter(3, java.sql.Types.VARCHAR);
            ResultSet rs = cs.executeQuery();

I am getting the following Exception -

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date


Modifications -
Replaced -
1. import java.sql.Date with import java.util.Date;
2.

cs.setDate(1,startDate);
cs.setDate(2,endDate);

by

cs.setDate(1,(java.sql.Date) startDate);
cs.setDate(2,(java.sql.Date) endDate);

Now I'm getting -

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

Can someone please help me out?

Dev1ce
  • 5,390
  • 17
  • 90
  • 150

4 Answers4

2

You can try implementing something in your code like this:

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); 

Date will parse the string that you put in and then it will give you teh correct date hope this helps :)

beastlyCoder
  • 2,349
  • 4
  • 25
  • 52
1

You've imported the wrong Date. Notice you've imported java.sql.Date which corresponds to the cast (Date), change that import to java.util.Date and it should work.

SimpleDateFormat.parse() returns an instance of java.util.Date which is different from java.sql.Date and you cannot cast from one to the other. It is possible to convert if you really need a java.sql.Date.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • that solved the exception but, now my startDate contains -> Wed Mar 15 00:00:00 IST 2017 , will this format satisfy my Stored Procedure's input format? – Dev1ce Apr 12 '17 at 18:30
1

you can convert from java.util.Date to java.sql.Date by using the constructor of the java.sql.Date:

Date(long date) //constructor of the java.sql.Date

Constructs a Date object using the given milliseconds time value.

to get the start date:

Date startDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(start).getTime());

to get the end date:

Date endDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(end).getTime());

note - in the future If you are going to be working with date-only values you should use the LocalDate class rather than java.util.Date.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Hi your solution worked, though now I'm getting the following Exception - java.sql.SQLException: Parameter number 1 is not an OUT parameter – Dev1ce Apr 12 '17 at 18:49
1

You can pass the (java.util.Date)Date obj into java.sql.Date constructor

java.util.Date startDate = new java.util.Date();
            java.sql.Date sqlDate = new java.sql.Date(startDate.getTime());
Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45