The following describes how to call Oracle functions in Java. In two cases, one is to call an Oracle function without an input parameter, and the other is to call an input parameter, an Oracle function that outputs parameters and returns a string.
Call a function without an input parameter
Function Definition
- CREATE OR REPLACE Function f_getstring Return Varchar2 Is
- Begin
- Return ''String value'';
- End f_getstring;
Java snippets for calling Functions
- CallableStatement cstmt = con.prepareCall("{?=call f_getstring}");
- cstmt.registerOutParameter(1, Types.VARCHAR);
- cstmt.execute();
- String strValue = cstmt.getString(1);
- System.out.println("The return value is:" + strValue);
- cstmt.close();
Calls a function that has an input parameter, an output parameter, and a string return value.
Function Definition
- CREATE OR REPLACE Function f_Getinfo(Id Integer, Age Out Integer) Return Varchar2 Is
- Begin
- Age := 10;
- Return ''The age is:'' || Id;
- End f_Getinfo;
Java code snippet for calling a function
- CallableStatement cstmt = con
- .prepareCall("{?=call f_getinfo(?,?)}");
- cstmt.registerOutParameter(1, Types.VARCHAR);
- cstmt.setInt(2, 11);
- cstmt.registerOutParameter(3, Types.INTEGER);
- cstmt.execute();
- String strValue = cstmt.getString(1);
- int age = cstmt.getInt(3);
- System.out.println("The return value is:" + strValue
- + " and age is:" + age);
- cstmt.close();
Edit recommendations]
Implementation of oracle function return table
How to uninstall an Oracle database in Windows
Multiple table Connection Methods in Oracle
Example of using SQL recursive statements in oracle
Comparison Between Common DB2 functions and Oracle Functions