1

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
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Ketaki
  • 31
  • 4
  • your are expecting a String here, but you could change the query to accept a Date too. EDIT : Oups, didn't see your removed the to_date into the PC – AxelH Nov 07 '16 at 11:59
  • Thanks you so much for your help. It worked. – Ketaki Nov 08 '16 at 11:08

1 Answers1

1
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.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73