Java调用Oracle的存储过程(实例)

 

/* 存储过程为
 * CREATE TABLE EmployeeInfo
(ID int,
name varchar2(50))
/
CREATE OR REPLACE PROCEDURE INSERT_PRO
is
BEGIN
    INSERT INTO EmployeeInfo VALUES(8,'asdf');
END;
/

CREATE OR REPLACE PROCEDURE myPro
(
n int,       --传给存储过程的参数 用set赋值
result out int       --存储过程返回的参数
)
is
i int:=0;
sum1 int:=0;
begin
    while i<n
    loop
    sum1:=sum1+1;
    i:=i+1;
    end loop;
    result:=sum1;
end;
 
*/


import java.sql.*;
import oracle.jdbc.OracleTypes;
import java.sql.CallableStatement;
public class JavaConnOracle {
//    JavaConnOracle() throws Exception {
//        Class.forName("oracle.jdbc.driver.OracleDriver");
//        Connection conn = DriverManager.getConnection(
//                "jdbc:oracle:thin:@localhost:1521:WJDB", "wjorac", "wangjun");
//        String procedure="{call INSERT_PRO}";
    ////无参调用
//        CallableStatement cstmt=conn.prepareCall(procedure);
//        cstmt.executeUpdate();
//    }
    
//******************************************888
    JavaConnOracle()throws Exception
    
{
        Class.forName(
"oracle.jdbc.driver.OracleDriver");
        Connection conn
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:WJDB","wjorac","wangjun");
        String procedure
="{call myPro(?,?)}";
        CallableStatement cstmt 
=conn.prepareCall(procedure);
        cstmt.setInt(
15000);   //给第一个参数赋值 
        cstmt.registerOutParameter(2,oracle.jdbc.OracleTypes.INTEGER);
        cstmt.execute();
        
//取出第二个参数的值.
        System.out.println("bbb  "+cstmt.getInt(2));
    }

    
public static void main(String args[])throws Exception {
        
new JavaConnOracle();
    }

}

posted @ 2008-04-09 15:44  wj-conquer  阅读(977)  评论(0编辑  收藏  举报