简体   繁体   中英

How to use JDBC4PreparedStatement with a MySQL stored procedure expecting an OUT parameter?

I am trying to call a stored procedure from Java using JDBC4PreparedStatement. The stored procedure takes in two input parameters and one output. I set the two input parameters by calling preparedStatement.setString(index, param)

but I tried the same for the output and get this error: OUT or INOUT argument 3 for routine db.deleteItem is not a variable or NEW pseudo-variable in BEFORE trigger

Is there a specific type I need to use? If so, how do I set it? I couldn't find any good examples.

Thanks

You should not use a prepared statement for that, but a callable statement instead. You can consider a callable statement a prepared statement with extra features to support stored procedures (including things like OUT parameters).

To register out parameters, you can use CallableStatement.registerOutParameter .

When we mark parameters as OUT or INOUT , the expectation is that the SP should be able to work with these parameters and at the same time it should be a Variable instead of a value. This variable then can be used at a later point in time.

A value for an OUT / INOUT parameter to the SP doesn't make much sense as the SP can't manipulate the value and hence the error. We just can pass values for IN parameters alone.

You can refer to this documentation here which states:

To get back a value from a procedure using an OUT or INOUT parameter, pass the parameter by means of a user variable, and then check the value of the variable after the procedure returns. (If you are calling the procedure from within another stored procedure or function, you can also pass a routine parameter or local routine variable as an IN or INOUT parameter.)

So, what you can do is something similar to the accepted answer here !

CALL alextest10 ('p99', 'Madeuppy', '999', 'w9', @a_message);
SELECT @a_message;

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM