Why do JDBC calls to stored procedures wrap the ca

2019-07-15 11:32发布

问题:

From Java we'd do something like: stmt.execute("SELECT * FROM users"); But for a stored procedure like: stmt.execute("{CALL createUser(?,?,?,?)}");

Why do we need the { and } when this is not required in normal SQL?

回答1:

The curly braces are the escape syntax for stored procedures. When the JDBC driver encounters {call createUser(?,?,?,?)} , it will translate this escape syntax into the native SQL used by the database to call the stored procedure.



回答2:

JDBC was derived from ODBC. Indeed the first jdbc driver was the jdbc to odbc bridge. So it made sense to use the ODBC syntax. The braces themselves indicate a function call, a stored procedure is basically a user defined function, so they are wrapped with the braces.



回答3:

Here is the official docs on JDBC escape syntax: https://docs.oracle.com/cd/E13157_01/wlevs/docs30/jdbc_drivers/sqlescape.html

And some vendor-specific descriptions:

  • Oracle
  • MS SQL Server
  • PostgreSQL
  • Derby


标签: java jdbc