- select d.*,
- cursor(select e.* from employees e where e.department_id = d.department_id) as employees
- from departments d;
Using Oracle 10g Express Edition's hr schema, I wanted all departments, with all the employees in each department in one call. Using the cursor keyword, I now have a ref cursor pointing to those employees per department record.
Enough already! Show me how to get this back in JDBC. Alright, since you were so nice about it. Its actually dirt simple.
- //... normal jdbc type execution code here
- //my result set object is called rs
- ResultSet employees = (ResultSet)rs.getObject("employees");
You simply use the getObject call using the ResultSet object returned by JDBC when executing a select statement, and then cast it to a ResultSet object, which in my example will be a ResultSet containing employee information.
So why should you use this? Um, because its fast and its easy. The way to get the most out of your database call is to get the most amount of data back at a time as you can. Using cursor expressions allows you to do that, without creating lots of procedures to return various ref cursors.
But Nick, what about all those open cursors won't I exceed max number of cursors on Oracle? The answer is no, you won't. You just have to remember to close those ResultSet object you are creating. So if you do get that message, check to make sure all your ResultSet's are being closed properly.
Have fun with your fast JDBC code, your customers will love you for it.