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

java调用存储过程返回的结果集(游标)

新建存储过程

create or replace procedure getcur(p_rc out sys_refcursor)
    is
     begin
          open p_rc for 'select * from room';
end getcur;

?java中调用

		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection(
				"jdbc:oracle:thin:@host:port:databasename", "user", "pwd");
		// DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

		CallableStatement callsta = null;
		ResultSet rs = null;
		try {
			callsta = conn.prepareCall("{call getcur(?)}");
			callsta.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
			callsta.execute();
			rs = (ResultSet) callsta.getObject(1);
			while (rs.next()) {
				System.out.println(rs.getString(1)+rs.getString(2));
			}
			rs.close();
			callsta.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
?
我的异常网推荐解决方案:oracle存储过程,http://www.aiyiweb.com/oracle-develop/177537.html