Calling a Postgres stored function SQL error

457 Views Asked by At

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??

2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

I use prepareCall to call stored procedures.

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