日期:2014-05-16  浏览次数:20465 次

JDBC 调用返回多条记录的存储过程(转)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;


public class InvokeProcedure {

    private static Connection getConnection() {
        Connection conn = null;
        String url ="jdbc:oracle:thin:@127.0.0.1:1521:demo";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, "scott", "tiger");
        } catch (Exception e) {
            System.out.println("");
            e.printStackTrace();
        }
        return conn;
       
    }
   
    private static Connection getsqlConnection() {
        Connection conn = null;
        String url ="jdbc:microsoft:sqlserver://localhost:1433;databaseName=y2";
        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            conn = DriverManager.getConnection(url, "sa", "");
        } catch (Exception e) {
            System.out.println("");
            e.printStackTrace();
        }
        return conn;
       
    }   
   
    //调用oracle的存储过程 --->  查询数据得到游标
    public static void  invokeProcedureResutl(){
        Connection conn =null;
        CallableStatement callableStatement =null;
        try {
            conn = getConnection();
            //第一步:获得CallableStatemen的实例;
            callableStatement = conn.prepareCall("{call p_result.pro_get_result(?,?,?,?)}");
           
            //第二步:为输出参数注册数据类型,为输入参数赋值;
            callableStatement.registerOutParameter(1,oracle.jdbc.driver.OracleTypes.CURSOR);
            callableStatement.setString(2, "emp");
            callableStatement.setInt(3, 2);
            callableStatement.setInt(4, 4);
            //第三步:执行存贮过程和获得返回值
            callableStatement.execute();
            ResultSet result =(Resu