简体   繁体   English

在java中调用pl / sql函数?

[英]Call pl/sql function in java?

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 number := 0;
BEGIN
   SELECT count(*) into t_canc
   FROM booking where status = 'CANCELLED';
   RETURN t_canc;
END;
/

To execute his in sql I use: 要在sql中执行他,我使用:

set serveroutput on
DECLARE
   c number;
BEGIN
   c := total_cancellations();
   dbms_output.put_line('Total no. of Cancellations: ' || c);
END;
/

My result is: 我的结果是:

anonymous block completed
Total no. of Cancellations: 1

My question is can someone help me call the function in JAVA, I have tried but with no luck. 我的问题是有人可以帮我调用JAVA中的函数,我试过但没有运气。

Java provides CallableStatements for such purposes . Java为此目的提供了CallableStatements

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. 将打印与pl / sql中相同的打印方式。 As per docs Connection#prepareCall() , 根据文档Connection#prepareCall()

Creates a CallableStatement object for calling database stored procedures. 创建CallableStatement对象以调用数据库存储过程。 The CallableStatement object provides methods for setting up its IN and OUT parameters, and methods for executing the call to a stored procedure. CallableStatement对象提供了用于设置其IN和OUT参数的方法,以及用于执行对存储过程的调用的方法。

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 ! 希望这可以帮助 !

Prepare a Callable Statement 准备一份可赎回的声明

There are two formats available, the familiar block syntax used by Oracle and the ANSI 92 standard syntax. 有两种格式,Oracle使用的熟悉的块语法和ANSI 92标准语法。 In the case of our sample program, the block syntax has the form: CallableStatement vStatement = vDatabaseConnection.prepareCall( "begin ? := javatest( ?, ? ); end;" ); 对于我们的示例程序,块语法的格式为: CallableStatement vStatement = vDatabaseConnection.prepareCall( "begin ? := javatest( ?, ? ); end;" );

The ANSI 92 syntax has the form: ANSI 92语法具有以下形式:

  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. total_cancellations不是过程或未定义的错误。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM