To create a function:
Create or Replace function My_fun (in_no in number) return VARCHAR2 is
Out_name VARCHAR2 (64);
Begin
Select Ename to Out_name from EMP where emp.empno=in_no;
return out_name;
End My_fun;
Java Program Demo:
Import java.sql.CallableStatement;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import Java.sql.Types;
public class Javacallmath {
public static void Main (string[] args) {
String driver= "Oracle.jdbc.driver.OracleDriver";
String url= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";
String user= "Scott";
String password= "Ztoracle";
try {
Class.forName (driver);
Connection c=drivermanager.getconnection (URL, user, password);
CallableStatement Cs=c.preparecall ("{=call my_fun (?)}");
Cs.setint (2, 9527);
Cs.registeroutparameter (1, Types.varchar);
Cs.execute ();
System.out.println (cs.getstring (1));
} catch (ClassNotFoundException e) {
throw new RuntimeException ();
} catch (SQLException e) {
}
}
}
Oracle PL/SQL how to invoke Oracle functions in Java