9

I am reading the JavaDocs for Connection#prepareCall:

sql - an SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is specified using JDBC call escape syntax.

According to this popular mkyong JDBC tutorial, I see the method executed like so:

String insertStoreProc = "{call insertDBUSER(?,?,?,?)}";
callableStatement = dbConnection.prepareCall(insertStoreProc);

I am wondering:

  1. Why is the string encapsulated in curly braces ({ ... })?
  2. Why does call proceed the name of the procedure?

Most importantly: is { call <nameOfProcedure> } the correct syntax for executing all stored procedures across all JDBC drivers? Or is { call ... } specific to a particular type of driver?

Are there variations of this procedure invocation syntax? For instance, are there scenarios/drivers where one might pass "{ execute <nameOfProcedure> }" into the prepareCall method? Is there documentation on any of this?


Update:

According to CallableStatement, JDBC offers 2 valid syntaxes for calling procs in a standard way, across all drivers:

{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}

And:

{call <procedure-name>[(<arg1>,<arg2>, ...)]}

But it is still unclear as to when to use either (that is: when to preprend call with ?=).

8
  • 6
    You should read java.sql.CallableStatement, where all these questions are answered. Dec 9, 2014 at 15:05
  • Thanks @LuiggiMendoza (+1) - please see my update and followup question regarding the use of ?=. Thanks again! Dec 9, 2014 at 15:12
  • 1
    Uh... read the proper JDBC driver for the specific database engine, where the usage of one or another is explained. Dec 9, 2014 at 15:19
  • 1
    That's how a stored procedure works. A SQL stored procedure can only return a single value as RETURN, and it could have OUT parameters. The return value from a stored procedure is different from the ResultSet(s) it could maintain opened when being called, but this is database engine specific. JDBC provides the most generic way to access to the database engine, but it depends on the database engine and on the JDBC driver implementation the support of all the features, specific vendor features or being an incomplete database engine. Dec 9, 2014 at 15:30
  • 1
    By the way, these are comments, not answers. Dec 9, 2014 at 15:31

1 Answer 1

5

The JDBC specification, section 13.4 Escape Syntax defines a number of escapes called JDBC escapes. These escapes are wrapped in curly braces. The specification for the call escape is:

If a database supports stored procedures, they can be invoked using JDBC escape syntax as follows:

{call <procedure_name> [(<argument-list>)]}

or, where a procedure returns a result parameter:

{? = call <procedure_name> [(<argument-list>)]}

The square brackets indicate that the (argument-list) portion is optional. Input arguments may be either literals or parameter markers. See “Setting Parameters” on page 108 for information on parameters.

This is also documented on java.sql.CallableStatement

So the name call and the curly braces are both specified in the JDBC specification.

As to the second part of your question. JDBC is a specification that tries to be database independent as much as possible and to do this it - usually - defaults to the SQL standard. IIRC the SQL specification specifies that a stored procedure either has no return value or a single return value. If the stored procedure has no return value, then you use the first call syntax. If the stored procedure has a single return value, then you use the second.

Stored procedures can also have OUT parameters (not to be confused with result sets), which are defined in the normal argument list.

2
  • Thanks @Mark Rotteveel (+1) - I wish I could upvote this more! One quick followup question: it seems that there are apparently three types of outputs JDBC supports from stored procedures: (1) Return Values, (2) Output Params and (3) Result Sets. Does the JDBC spec weigh-in on where/when/how these outputs should be used? In other words, is there a JDBC standard for what constitutes a Return Value vs Output Param vs Result Set? Thanks again so much! Dec 9, 2014 at 16:46
  • @IAmYourFaja This isn't really specified in JDBC, I believe you need to look at the SQL specification for that. But in general: return value: the single value returned by a RETURN value statement in a SP, OUT parameters get a value once in the SP (or at least: the last value assigned to the OUT parameter is the one you get back). Result sets are just that: rows returned from the stored procedure (multiple result sets are possible, if the DB supports it!). ResultSet fields are not supposed to be defined as parameters. Dec 9, 2014 at 16:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.