Loading

java调用oracle存储过程,返回结果集

package com.srie.db.pro;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class CallPro {

    public static void main(String[] args) {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            String url = "jdbc:oracle:thin:@aaa";
            String user = "a";
            String password = "a";
            conn = DriverManager.getConnection(url, user, password);

            String sql = "{call pro_getchart(?)}";
            CallableStatement statement = conn.prepareCall(sql);
            statement.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
            statement.execute();
            ResultSet rs = null;
            // 方法1:
            //rs = ((oracle.jdbc.OracleCallableStatement) statement).getCursor(1);
            // 方法2:
            rs = (ResultSet) statement.getObject(1);
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

}

方法1:

http://www.2cto.com/kf/201109/105741.html

方法2:

http://www.cnblogs.com/icerainsoft/archive/2011/08/24/2152381.html

未验证内容:

http://blog.csdn.net/baykermr/article/details/4664268

posted @ 2015-07-21 14:49  stono  阅读(318)  评论(0编辑  收藏  举报