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)