1

I am trying to run a Stored Procedure in MSSQL via jdbc. The way I see it I have two options:

Connection conn = //Code that I know gets the correct connection
Statement stmt = conn.prepareStatement(request.query);
ResultSet resultSet = stmt.executeQuery(request.query);

Using this the query runs fine but the code throws an exception: "Error: The statement did not return a result set."

Or

PreparedStatement stmt = conn.prepareStatement(request.query);
ResultSet resultSet = stmt.executeQuery();

Which does the same thing. Are these essentially doing the same thing under the hood? I know I could use stmt.executeUpdate() but I want to get a ResultSet back so I can do post processing.

Which way is correct? And what can I do to guarantee a ResultSet is returned?

I looked at this and this question and the answers were not effective/applicable.

Community
  • 1
  • 1
ford prefect
  • 7,096
  • 11
  • 56
  • 83

1 Answers1

1

To run a stored procedure you should use a CallableStatement. Note that you need to use the {call ...} syntax, something like this:

CallableStatement cs = conn.prepareCall("{call procedure_name}");
ResultSet rs = cs.executeQuery();
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418