M. Mashayekhi M. Mashayekhi - 15 days ago 4
Java Question

Calling a MSSQL stored procedure in Java

I have access to a stored procedure on a sql server which has one parameter and I can easily run it on the sql client as follow:

exec sp_name "2016/11/01"


Now I want to do the same thing in java.

PreparedStatement ps = conn.prepareStatement("sp_name ?");
ps.setString(1, "2016/11/01");
ResultSet rs = ps.executeQuery();


In
rs
I can see the columns' names, but zero row is returned. I think it is because of the stored procedure's parameter. Am I missing something here?

Answer

Here is the code that worked eventually:

String date= "2016/11/01"
String queryString "exec sp_dmp_pub_status ?";
PreparedStatement ps = conn.prepareStatement(queryString);         
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date parsed = format.parse(date);
java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());
ps.setDate(1, sqlDate);        
ResultSet rs = ps.executeQuery();