首页 / 操作系统 / Linux / 在Java语言中调用存储函数
连接Oracle数据库
- private static Connection conn;
- static{
- //第一步:加载驱动
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- //得到连接对象 conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","scott");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
实例一:
- 【
- create or replace function sumSal(emp_no number)--function(参数的值 必须有类型)
- --返回值类型
- return number--必须有返回值
- as
- --声明变量
- emp_sal emp.sal%type;
- emp_comm emp.comm%type;
- total emp.sal%type;
- begin
- select sal,comm into emp_sal,emp_comm from emp where empno=emp_no;
- total:=emp_sal*12+nvl(emp_comm,0);
- return total;--必须返回 返回值类型一定相同
- end;
- 】
- public static void functionTest1() throws SQLException{
- //mypackage 存储函数
- CallableStatement cas=conn.prepareCall("{?=call sumSal(?)}");
- //从1开始
- int index = 1;
- cas.registerOutParameter(index++, oracle.jdbc.OracleTypes.NUMBER);
- //为占位符赋值
- cas.setInt(index++,7369);
- boolean flag=cas.execute();
- System.out.println(flag);
- System.out.println(cas.getInt(1));
- }