Hello.
I have a question regarding question nr 15, from the chapter 21 (
JDBC).
Here is the question itself:
Suppose learn() is a stored procedure that takes one IN parameter and one OUT parameter. What is wrong with the following code? (Choose all that apply.)
18: var sql = "{?= call learn(?)}";
19: try (var cs = conn.prepareCall(sql)) {
20: cs.setInt(1, 8);
21: cs.execute();
22: System.out.println(cs.getInt(1));
23: }
A. Line 18 does not call the stored procedure properly.
B. The parameter value is not set for input.
C. The parameter is not registered for output.
D. The code does not compile.
E. Something else is wrong with the code.
F. None of the above. This code is correct.
Boyarsky, Jeanne; Selikoff, Scott. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 1064). Wiley. Kindle Edition.
And here is the correct answer, mentioned by the author in Appendix:
C. Since an OUT parameter is used, the code should call registerOutParameter(). Since this is missing, option C is correct.
Boyarsky, Jeanne; Selikoff, Scott. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 1174). Wiley. Kindle Edition.
I'm wondering why
A is not considered as well a correct answer?
Because, the exercise is clearly told that
we have 2 parameters: one IN and one OUT.
In the sql
String, we have just one parameter indicated in "(?)", instead of 2.
This is a mistake, from my point of view, and will lead to SQLException in the runtime.
Parameters, regardless of their type (IN, OUT, INOUT), need to be declared in parentheses "()" of the called stored procedure.
In the same time, OUT parameters for readability, could be optionally as well declared before call using "
?="
, but it's not mandatory.
Please, confirm me, if I'm right, or give a comment otherwise.
Thank you very much.