Oracle存储过程调用 java程序 --in/out 输出模式

来源:互联网 发布:淘宝如何应对无理投诉 编辑:程序博客网 时间:2024/06/04 18:32
package hu.test;

import static org.junit.Assert.*;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import hu.db.OracleDatabase;

import org.junit.Test;

/* 存储过程 oracle调用  -----存储过程在数据库中
 create or replace procedure proce_in(v_empno in emp3.empno%type,v_ename out emp3.ename%type)
 as  
 begin
 select  ename into v_ename from emp3 where empno = v_empno;
 dbms_output.put_line('名称是'||v_ename);
 end;

//这是在数据库调用存储过程
 // set serveroutput on;
 // set verify off; 开启pl/sql客户端输出显示
 declare
 v_ename1 emp3.ename%type;
 v_empno1 emp3.empno%type;
 begin
 v_empno1:=&empno;
 proce_in(v_empno1,v_ename1);
 dbms_output.put_line('名称是'||v_ename1);
 end;
 */
public class ConnectionTest {

    @Test
    public void test() {

        System.out.println(OracleDatabase.getConnection() != null);
    }

    @Test
    public void test_procedure() {
        
        Connection con = OracleDatabase.getConnection();//创建链接
        CallableStatement call = null;
        try {
            call = con.prepareCall("{call proce_in(?,?)}");//调用存储过程
            call.setInt(1, 7844);//设置存储过程参数----------in输入参数
            call.registerOutParameter(2, Types.VARCHAR);//设置输出参数-----out 注册输出参数   (参数位置,参数类型)
            call.execute();//执行存储过程调用
            String ename = call.getString(2);//获取存储过程的输出参数 参数位置
            System.out.println(ename);
            call.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

0 0
原创粉丝点击