Call pl/sql function in java?

前端 未结 2 1886
死守一世寂寞
死守一世寂寞 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:34

    Prepare a Callable Statement

    There are two formats available, the familiar block syntax used by Oracle and the ANSI 92 standard syntax. In the case of our sample program, the block syntax has the form: CallableStatement vStatement = vDatabaseConnection.prepareCall( "begin ? := javatest( ?, ? ); end;" );

    The ANSI 92 syntax has the form:

       CallableStatement vStatement = 
               vDatabaseConnection.prepareCall( "{ ? = call javatest( ?, ? )}");
    

    source

    If you receive the below error, you might want to use the first format.

    total_cancellations is not a procedure or is undefined error.

    Sample code.

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@xx.xxx.xx.xxx:1521:xxx", "user","pass");
    CallableStatement cstmt = conn.prepareCall("begin ? := TEST_FUNC(?,?); end;");
    cstmt.registerOutParameter(1, Types.INTEGER);
    cstmt.setString(2, "Test");
    cstmt.setInt(3, 1001);
    cstmt.execute();
    int result = cstmt.getInt(1);
    System.out.print("Result: " + result);
    cstmt.close();
    conn.close();
    

提交回复
热议问题