简体   繁体   中英

JDBI supports stored procedure with out parameter

JDBI has @SqlCall annotation for calling procedure, but right now the method marked by this annotation only can return null or OutParameters .

So right now is there anyway we can get the out parameter via OutParameters when calling the procedure with out parameter like below:

CREATE PROCEDURE get_total (IN num1 INT, IN num2 INT, OUT result INT) 
BEGIN
   SELECT num1 + num2 INTO result;
END;

So right now my temporary solution is:

@SqlCall("call get_total(:num1, :num2, @result)")
public abstract void getTotal(@Bind("num1") int num1, @Bind("num2") int num2);

@SqlQuery("SELECT @result AS result")
public abstract int getQueryTotal();

/**
 * call procedure firstly, then query the out parameter as a variable.
 */
public int getCalculateTotal(int num1, int num2) {
   getTotal(1, 2);
   return getQueryTotal();
}

So is there anyone has a good way to get the out parameters?

You can register an out parameter with the @OutParameter annotation:

@SqlCall("call get_total(:num1, :num2, :result)")
@OutParameter(name="result", sqlType=Types.INTEGER)
public abstract OutParameters getTotal(@Bind("num1") int num1, @Bind("num2") int num2);

The OutParameters type has methods to get each out-parameter by name.

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