mizech mizech - 9 months ago 30
Java Question

Java JDBC: Why is it necessary to register out-parameter?

I've got to learn Java JDBC currently.

Today I had a look on how Stored Procedures are called from within JDBC.

What I don't get ..., when I have a Stored Procedure like for example this one:

CREATE PROCEDURE demo.get_count_for_department
(IN the_department VARCHAR(64), OUT the_count INT)
BEGIN
...


"the_count" is marked as an out parameter. Type is also specified. So this should all be known.

Nevertheless I have to specify the type again

statement.registerOutParameter(2, Types.INTEGER);


I have to put the type in there again? It seems redundant to me.

Why do I have to give two parameter in there at all?

statement = connection.prepareCall("{call get_count_for_department(?, ?)}");


I haven't seen this in any other programming language. You only have to take care for the in-parameter. For the out-parameter takes the function care itself.

Why is that different here?

Perhaps someone can drop me a few lines. So that I get a better idea about how those Stored Procedure-calls work.

Answer Source

Depending on the database it might technically not be necessary. Doing this allows a JDBC driver to execute the stored procedure without first having to query the database for metadata about the statement, and it can also be used to disambiguate between multiple stored procedures with the same name (but different parameters).