Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 353
Troy Young Avatar asked Oct 31 '22 02:10

Troy Young


1 Answers

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.

like image 75
qualidafial Avatar answered Nov 04 '22 04:11

qualidafial