[英]Example for calling a stored procedure in Oracle SQL from Java using JDBC
I have seen examples for calling procedures where the return type is a number, by using CallableStatement
and then setting the IN
parameters and registering the OUT
parameters based on the return type. 我已经看到了一些示例,这些示例通过使用
CallableStatement
返回类型为数字的过程,然后设置IN
参数并根据返回类型注册OUT
参数。
But how to do the same, if the OUT
parameter is a table returned by a SELECT
statement? 但是,如果
OUT
参数是SELECT
语句返回的表,该如何做呢?
I have tried to find some examples but of no use. 我试图找到一些例子,但没有用。 Please help.
请帮忙。
This is some example code where the OUT
parameter is an integer 这是一些示例代码,其中
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);
But I want an example where the OUT
parameter is a table ( SYS_REFCURSOR
). 但是我想要一个示例,其中
OUT
参数是一个表( SYS_REFCURSOR
)。
You need to declare your first parameter as a REF_CURSOR 您需要将第一个参数声明为REF_CURSOR
cstmt.registerOutParameter (1, oracle.jdbc.OracleTypes.CURSOR);
Here is a complete example that uses JDBC-OCI but you can use JDBC-thin (in fact you should): https://docs.oracle.com/cd/A97335_02/apps.102/a83724/samapp5.htm 这是一个使用JDBC-OCI的完整示例,但您可以使用JDBC-thin(实际上应该使用): https : //docs.oracle.com/cd/A97335_02/apps.102/a83724/samapp5.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.