0

Im calling a stored function like the following:

PreparedStatement deleteAll=connection.prepareStatement("{ call delete_all_data() }");
deleteAll.execute();

And in the logs i see:

15:16:31,950  WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 42601
15:16:31,950 ERROR SqlExceptionHelper:144 - ERROR: syntax error at or near "{"
  Position: 1

what is wrong with the prepareStatement??

3
  • 1
    You should use CallableStatement for stored procedures. Commented Nov 24, 2014 at 15:23
  • connection.prepareStatement("select delete_all_data()") is enough. No CallableStatement needed Commented Nov 24, 2014 at 15:58
  • @LuiggiMendoza A JDBC driver should also support the call escape with Statement and PreparedStatement. This should work as long as the stored procedure doesn't have OUT parameters (PreparedStatement) or parameter placeholders (for Statement). However in practice most drivers only support it with CallableStatement. Commented Nov 24, 2014 at 16:26

2 Answers 2

2

Change connection.prepareStatement (which expects SQL) to connection.prepareCall. That may very well be the only change you need, as a CallableStatement is a PreparedStatement.

Sign up to request clarification or add additional context in comments.

1 Comment

See my comment on the question itself, although this is probably the solution, it is not according to specification: the call escape should also be supported on Statement and PreparedStatement. See: section 6.4 of JDBC 4.2 and mail.openjdk.java.net/pipermail/jdbc-spec-discuss/2013-January/…
2

I use prepareCall to call stored procedures.

String SQL = "{call delete_all_data()}";
cstmt = conn.prepareCall (SQL);

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.