使用JDBC从Java调用Oracle SQL中的存储过程的示例

问题描述 投票:0回答:1

我已经看到了调用返回类型为数字的过程的示例,使用CallableStatement然后设置IN参数并根据返回类型注册OUT参数。

但是如果OUT参数是SELECT语句返回的表,怎么做呢?

我试图找到一些例子,但没有用。请帮忙。

这是一些示例代码,其中OUT参数是一个整数

  // Prepare to call the stored procedure RAISESAL.
  // This sample uses the SQL92 syntax
    CallableStatement cstmt = conn.prepareCall ("{? = call RAISESAL (?, ?)}");
  // Declare that the first ? is a return value of type Int
     cstmt.registerOutParameter (1, Types.INTEGER);

    // We want to raise LESLIE's salary by 20,000
    cstmt.setString (2, "LESLIE");  // The name argument is the second ?
    cstmt.setInt (3, 20000);        // The raise argument is the third ?

    // Do the raise
    cstmt.execute ();

    // Get the new salary back
    int new_salary = cstmt.getInt (1);

    System.out.println ("The new salary is: " + new_salary);

但我想要一个例子,其中OUT参数是一个表(SYS_REFCURSOR)。

java oracle stored-procedures
1个回答
0
投票

您需要将第一个参数声明为REF_CURSOR

cstmt.registerOutParameter (1, oracle.jdbc.OracleTypes.CURSOR);

这是一个使用JDBC-OCI的完整示例,但您可以使用JDBC-thin(事实上您应该):https://docs.oracle.com/cd/A97335_02/apps.102/a83724/samapp5.htm

© www.soinside.com 2019 - 2024. All rights reserved.