建立函数:
create or replace function my_fun(in_no in number) return varchar2 is
out_name varchar2(64);
begin
select ename into out_name from emp where emp.empno=in_no;
return out_name;
end my_fun;
java程序演示:
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) {
}
}
}