Call pl/sql function in java?

前端 未结 2 1888
死守一世寂寞
死守一世寂寞 2020-12-11 04:07

So I\'ve got a function that checks how many cancellations are in my booking table:

CREATE OR REPLACE FUNCTION total_cancellations
RETURN number IS
   t_canc         


        
2条回答
  •  醉酒成梦
    2020-12-11 04:42

    Java provides CallableStatements for such purposes .

    CallableStatement cstmt = conn.prepareCall("{? = CALL total_cancellations()}");
    cstmt.registerOutParameter(1, Types.INTEGER);
    cstmt.setInt(2, acctNo);
    cstmt.executeUpdate();
    int cancel= cstmt.getInt(1);
    System.out.print("Cancellation is "+cancel);
    

    will print the same as you do in the pl/sql. As per docs Connection#prepareCall(),

    Creates a CallableStatement object for calling database stored procedures. The CallableStatement object provides methods for setting up its IN and OUT parameters, and methods for executing the call to a stored procedure.

    You can also pass parameters for the function . for ex ,

    conn.prepareCall("{? = CALL total_cancellations(?)}");
    cstmt.setInt(2, value);
    

    will pass the values to the function as input parameter.

    Hope this helps !

提交回复
热议问题