java调用Oracle存储过程返回Cursor

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ouyanggengcheng/article/details/77976755
1.创建包
create or replace package pack1 is
type test_cursor is ref cursor;
end;


2.创建存储过程
create or replace procedure pro1(test_uNo in number,test_res out pack1.test_cursor) is
begin
open test_res for select * from user;
end;


3.数据库工具类,这里只列举了部分功能
public class MysqlHelper {
//加载驱动
    private static String DRIVER = "";
    //定义数据库账号
    private static String USERNAME = "";
    //定义数据库密码
    private static String PASSWORD = "";
    //访问的地址
    private static String URL = "";
    //定义数据库的连接
    private Connection connection = null;
//定义sql语句的执行对象,用PreparedStatement可以预防or 1=1,而Statement是存在注入漏洞的
    private PreparedStatement pStatement = null;
    //定义查询返回的结果集合
    private ResultSet resultset = null;
    //定义存储过程执行对象
    private CallableStatement clstatement = null;
    
    private static InputStream fis = null;
    
    
    static{
    Properties props = new Properties();  
    try {
fis = MysqlHelper.class.getClass().getResourceAsStream("/test.properties");
props.load(fis);
DRIVER = props.getProperty("connection.driver_class");
USERNAME = props.getProperty("connection.username");
PASSWORD = props.getProperty("connection.password");
URL = props.getProperty("connection.url");
//只注册一次
Class.forName(DRIVER);//注册驱动
System.out.println("我是静态取值的方法,等一下测试一下,是先执行这个static方法再执行构造函数");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("读取配置文件异常");
}
    }
    
    public MysqlHelper()
    {
        try {
        System.out.println("我是连接");
            connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);//定义连接
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }


/**
     * 执行存储过程,返回值
     * @param sql  执行的存储过程sql
     * @param inparams   输入的参数
     * @param outparams  输出的参数
     * @return   返回CallableStatement
     */
    public CallableStatement callProcedureReturnCursor(String sql,String[] inparams,int[] outparams){
   
    try {
    clstatement = connection.prepareCall(sql);
    if(inparams != null){
    for(int i=0;i<inparams.length;i++){
    clstatement.setObject(i+1, inparams[i]);
    }
    }
   
    if(outparams != null){
    for(int i=0;i<outparams.length;i++){
    //注册的位置,则是输入参数的大小+1再加i,因为i可能是多个值
    clstatement.registerOutParameter(inparams.length+1+i, outparams[i]);
    }
    }
    clstatement.execute();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
//抛出运行异常
throw new RuntimeException(e.getMessage());
}finally{
//不需要关闭
}
   
    return clstatement;
    }
    
    /**
     * 注意在finally里面执行以下方法,关闭连接
     */
    public void closeConnection(ResultSet resultset,Statement pStatement,Connection connection)
    {
        if(resultset != null)
        {
            try{
                resultset.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(pStatement != null)
        {
            try {
                pStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    
    //提供get方法,便于调用工具类的资源关闭
    public Connection getConnection() {
return connection;
}
public PreparedStatement getpStatement() {
return pStatement;
}
public ResultSet getResultset() {
return resultset;
}
public CallableStatement getClstatement() {
return clstatement;
}
}


4.调用工具类
public void testCallPro(){
ResultSet rs = null;
MysqlHelper mysqlhelper = new MysqlHelper();
try {
String sql = "{call pro1(?,?)}";
String testin[] = {"95"};
//这里传入的是输出的类型,输出是Cursor
int testout[] = {oracle.jdbc.OracleTypes.CURSOR};

CallableStatement cs = mysqlhelper.callProcedureReturnCursor(sql, testin, testout);
//第二个值为输出值
rs = (ResultSet)cs.getObject(2);

while(rs.next()){
System.out.println(rs.getInt(1)+""+rs.getString(2));
}
} catch (Exception e) {
// TODO: handle exception
}finally{
//关闭连接
mysqlhelper.closeConnection(rs, mysqlhelper.getClstatement(), mysqlhelper.getConnection());
}


}

猜你喜欢

转载自blog.csdn.net/ouyanggengcheng/article/details/77976755