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

package jp.co.kindlingsoft;

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

public class TestDB {

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {

		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:KIN";
		String user = "KIN";
		String password = "KIN";

		Connection con = null;
		CallableStatement stmt = null;
		ResultSet rs = null;

		Class.forName(driver);

		con = DriverManager.getConnection(url, user, password);
		stmt = con.prepareCall("{CALL GET_DATA(?,?)}");
		stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
		stmt.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
		stmt.execute();

		rs = (ResultSet) stmt.getObject(1);
		while(rs.next()) {
			System.out.println(rs.getObject(1)); // rs.getObject(column_name)
		}

		System.out.println("************************************************");

		rs = (ResultSet) stmt.getObject(2);
		while(rs.next()) {
			System.out.println(rs.getObject(1));
		}
	}
}


CREATE OR REPLACE PROCEDURE GET_DATA(
CUR_OUT_1 OUT SYS_REFCURSOR,
CUR_OUT_2 OUT SYS_REFCURSOR) 
IS

BEGIN
	OPEN CUR_OUT_1 FOR 
		SELECT USER_TAB_COLS.COLUMN_NAME
		FROM USER_TAB_COLS
		WHERE TABLE_NAME = 'TABLE1';
		
	OPEN CUR_OUT_2 FOR 
		SELECT USER_TAB_COLS.COLUMN_NAME
		FROM USER_TAB_COLS
		WHERE TABLE_NAME = 'TABLE2';
END GET_DATA


你可能感兴趣的:(Java语言)