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.

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.