Search code examples
javato-datecallable-statement

Java-To_Date() inside Callable Statement


I need to execute the below procedure with 2 input param in Java. It should get executed successfully, there is no output param required.

Can anyone please help me with the code.

SQL Statement:

call pack_context.context_open(to_date('31-JULY-2016'),7);

Java Code:

CallableStatement callableStatement = null;
String proc = "{call pack_context.context_open(?,?)}";
callableStatement = con.prepareCall(proc);          
callableStatement.setInt(2, 7);
callableStatement.setDate(parameterName, x);//Need Help

Solution

  • callableStatement.setDate(parameterName, x);//Need Help
    

    can be adjusted as shown below:

    String myDate="31-JUL-2016"; // notice JUL, instead of JULY
    // or use some other date string like yyyy-MM-dd
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
    java.util.Date date = sdf.parse(myDate);
    java.sql.Date d = new java.sql.Date(date.getTime());
    callableStatement.setDate(parameterName, d);
    // here parameterName should be the exact name as 
    // in your procedure `pack_context.context_open`.
    

    setDate(String parameterName, Date x) requires the second parameter to be of the type java.sql.Date. The driver converts this to an SQL DATE value when it sends it to the database.