Question

accessing a PL/SLQ cursor from within a Java program

Asked by: mhci_nne

can I access a PL/SLQ cursor from within a Java program (On the middle-tier)?
If so can someone please show me a simple example?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-03-12 at 07:54:05ID22443451
Tags

java

,

cursor

Topics

JDeveloper

,

PL / SQL

,

Microsoft Windows Operating Systems

Participating Experts
1
Points
500
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PL/SQL Cursor within a Cursor
    I am attempting to write PL/SQL code to have two cursors in one procedure. The first cursor selects a fund code. I want to pass the results of the first cursor into the second cursor. So, I want to know if I can have a cursor within a cursor in the same procedure looping at 2...
  2. JSP or PL/SQL
    I have to develop a pretty big Internet site (with frequent data base access) and I would like to know is it better to use PL/SQL or JSP? Is there a reference/link where I can get this info (PL/SQL Vs. JSP)? What are the advantages and disadvantages of both languages ?
  3. Cursor
    Hi Folks, Can I retrun a cursor from a function. All I want to do is, if I can return a cursor from a function then I can set this to a recordset in VB and populate an excel sheet. Pls be specific and see if you can share some code Thanks Ashish
  4. Converting PL/SQL table into a CURSOR
    Hi there, Is it possible to Converting PL/SQL table into a CURSOR?
  5. Ref Cursor from PL/SQL table?
    How to return the data in a PL/SQL table to a REF Cursor. The PL/SQL table is created on a Record type. Thanks
  6. cursors
    Implicit and explicit cursors, i can use cursors on both ways like for for cursors and opening , fetching and closing cursors. which one is implicit and which one is explicit, is that implicit cursor where i can only use DML, but not in explicit. implicit cursors can't be ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: slightwvPosted on 2007-03-12 at 08:09:38ID: 18702538

Do you mean a ref cursor being returned from a stored procedure?

Check out:
http://www.databasedesign-resource.com/oracle-ref-cursor-in-java.html

 

by: mhci_nnePosted on 2007-03-12 at 08:28:56ID: 18702712

yes that exactly what I'm thinking about, I'll just take a look at the link and tehn return.

By they way, is that not the best way to do it?

 

by: mhci_nnePosted on 2007-03-12 at 08:34:48ID: 18702785

cool so it's just return as a resultset, you just have to remember to set teh fetch size.
otherwise is just like executing normal queries?

 

by: slightwvPosted on 2007-03-12 at 09:27:30ID: 18703324

>>is that not the best way to do it?

I can't think of a better way off the top of my head.

>>otherwise is just like executing normal queries

I guess so.  I'm not a java person.

 

by: mhci_nnePosted on 2007-03-12 at 10:07:07ID: 18703726

thanks, neither can I  :-)

By the way, is there a simple way to do exception handling within PL/SQL when working with REF CURSER?

 

by: slightwvPosted on 2007-03-12 at 10:52:24ID: 18704119

>> is there a simple way to do exception handling within PL/SQL

In what way?  You can handle the exceptions in the stored proc and possible raise your own custom exception but I'm not sure what you are looking for.  If you can explaint it a little better, I'll try to help.

 

by: mhci_nnePosted on 2007-03-13 at 01:43:53ID: 18708545

Thanks
I'm relatively new to to REF CURSOR (well pl/sql in generel), but I was wondering what to return when using REF CURSOR. As an example this wouldn't work, since I need to return a REF cusor and not NULL:

EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
           RETURN 'NO DATA FOUND';
   WHEN TOO_MANY_ROWS
   THEN
          RETURN NULL;
   WHEN OTHERS
   THEN
          RETURN NULL;

 

by: mhci_nnePosted on 2007-03-13 at 04:35:53ID: 18709166

I have tested the java code with my REF CURSOR function (without exception handling within PL/SQL). The exception handling is created within java and it works fine, but I would also like to be able to do it in PL/SQL.

When I call the PL/SQL function with an invalid value I receive "ORA-01722: invalid number", just as i'm suppose to. But I think it would be best if I was able to do the exception handling from within PL/SQL since it would increase reusability.

 

by: slightwvPosted on 2007-03-13 at 06:11:09ID: 18709746

Since the code stub you provided has a RETURN, I'm assuming a function and not a procedure.

You should be able to return null.  I'm not sure why this isn't working.  If you can provide a little more detail I'll attempt to assist you.

Since this is getting a little more complicated, I'll unaccept the answer to this question and let other experts assist.

 

by: mhci_nnePosted on 2007-03-13 at 06:13:57ID: 18709773

CREATE OR REPLACE PACKAGE MHCI_poTypes
IS
      TYPE ref_cursor IS REF CURSOR;
      FUNCTION MHCI_PO_LINES(
        paramHEADER_ID VARCHAR, paramPROJECT_ID VARCHAR, paramTASK_ID VARCHAR) RETURN ref_cursor;
      FUNCTION MHCI_PO_HEADERS(
            paramHEADER_ID VARCHAR, paramPO_LINE_ID VARCHAR) RETURN ref_cursor;      
END MHCI_poTypes;

CREATE OR REPLACE package body MHCI_poTypes
IS
      FUNCTION MHCI_PO_LINES(
            paramHEADER_ID VARCHAR, paramPROJECT_ID VARCHAR, paramTASK_ID VARCHAR) RETURN ref_cursor
      IS
            csr_poLines_ref ref_cursor;
      BEGIN
            OPEN csr_poLines_ref FOR
                  SELECT
                     PAT.TASK_NUMBER TASK
                     ,POL.LINE_NUM DOC
                     ,'Missing' CP_Agr
                     ,'Missing' STATUS
                     ,POL.CREATION_DATE
                     ,(POLA.UNIT_PRICE * POLA.QUANTITY) REVISION_AMT
                     ,((POLL.QUANTITY - POLL.QUANTITY_CANCELLED) * POL.UNIT_PRICE) TOTAL_AMT
                     ,(POLL.QUANTITY_RECEIVED * POL.UNIT_PRICE) RECIEVED_AMT
                     ,(POLL.QUANTITY_BILLED * POL.UNIT_PRICE) INVOICE_AMOUNT
                     ,(POLL.QUANTITY_CANCELLED * POL.UNIT_PRICE) CANCELLED_AMT
                  FROM
                     PA_TASKS PAT
                     ,PO_LINES_ALL POL
                     ,PO_LINE_LOCATIONS_ALL POLL
                     ,po_lines_archive_ALL POLA
                  WHERE
                     POLA.PO_HEADER_ID = POL.PO_HEADER_ID
                     AND POLA.PO_LINE_ID = POL.PO_LINE_ID
                     AND POLL.PO_LINE_ID = POL.PO_LINE_ID
                     AND POL.PO_HEADER_ID  = paramHEADER_ID
                     AND PAT.PROJECT_ID = paramPROJECT_ID
                     AND PAT.TASK_ID = paramTASK_ID;
            RETURN csr_poLines_ref;
      END MHCI_PO_LINES;
      
      FUNCTION MHCI_PO_HEADERS(
            paramHEADER_ID VARCHAR, paramPO_LINE_ID VARCHAR) RETURN ref_cursor
      IS
            csr_poHeader_ref ref_cursor;
      BEGIN
            OPEN csr_poHeader_ref FOR
            SELECT
                  'Missing' BUYER
                  ,POH.SEGMENT1 DOC
                  ,POH.AUTHORIZATION_STATUS STATUS
                  ,POH.CREATION_DATE CREATION_DATE -- DD-MM-YYYY HH:MM:SS
                  ,'Missing' REVISION_AMT
                  ,((POLL.QUANTITY - POLL.QUANTITY_CANCELLED) * POL.UNIT_PRICE) TOTAL_AMT
                  ,(POLL.QUANTITY_RECEIVED * POL.UNIT_PRICE) RECIEVED_AMT
                  ,(POLL.QUANTITY_BILLED * POL.UNIT_PRICE) INVOICE_AMOUNT
                  ,(POLL.QUANTITY_CANCELLED * POL.UNIT_PRICE) CANCELLED_AMT
            FROM
                  PO_HEADERS_ALL POH,
                  PO_DISTRIBUTIONS_ALL POD,
                  PO_LINES_ALL POL,
                  PO_LINES_ARCHIVE_ALL POLA,
                  PO_LINE_LOCATIONS_ALL POLL
            WHERE
                  poh.po_header_id = pol.po_header_id
                  AND pol.po_line_id = poll.po_line_id
                  AND poll.po_line_id = pod.po_line_id
                  AND POH.PO_HEADER_ID = paramHEADER_ID
                  AND POLA.PO_LINE_ID = paramPO_LINE_ID;
            RETURN csr_poHeader_ref;
      END MHCI_PO_HEADERS;
END MHCI_poTypes;

 

by: mhci_nnePosted on 2007-03-13 at 06:16:12ID: 18709789

that's the package without the exception handling. Could you show me an example how to do the eception handling then it would really cool :-)

 

by: slightwvPosted on 2007-03-13 at 06:29:40ID: 18709919

I'm still not sure what you want to do with the internal exception handling.  Assuming you can return a NULL from the function, then what.  All you will know is that 'something' happened.

Also, what version of Oracle (please provide all 4 number.  ex./ 10.2.0.3).

 

by: slightwvPosted on 2007-03-13 at 06:30:16ID: 18709925

Are you done with this question?  If not, why did you accept an answer?

 

by: mhci_nnePosted on 2007-03-13 at 06:31:50ID: 18709943

sorry, no I would like you to show the exception handling

 

by: mhci_nnePosted on 2007-03-13 at 07:57:47ID: 18710806

The Oracle Database server I'm using is 10g...

 

by: slightwvPosted on 2007-03-13 at 08:53:28ID: 18711296

I've unaccepted the answer so others can participate.

Back to the question:
I can't help you with exception handling until you explain what it is you want to do.  I need specifics not just: "I want to handle exceptions in PL/SQL".

 

by: mhci_nnePosted on 2007-03-13 at 09:30:28ID: 18711696

I would like my java program to be able to receive a meningfull error message from a PL/SQL function.
 I only been using exception handling in simple PL/SQL functions using static cursors, howevery I don't know how to do it using REF CURSOR.
 
Two simple examples should do the trick,
1) One using a predefined exception like TOO_MANY_ROWS
2) One using a custom defined exceptoin

By the way I don't need any java code, just the  PL/SQL code.
Thanks :-)

 

by: mhci_nnePosted on 2007-03-13 at 09:34:49ID: 18711749

By the way just use a "Hello world" example since the code I have i working fine (I just need to add the exception handling :-)

 

by: slightwvPosted on 2007-03-13 at 10:11:12ID: 18712064

I think I'm starting to understand.  I'm slow at times.

You can trap no_data_found and the custom 'Too many Rows' but the only way I can think of requires an additional execution of the statement.

See if this helps:
-----------------------------------------
drop table tab1;
create table tab1 (
      col1 number
);

insert into tab1 values('1');
insert into tab1 values('2');
insert into tab1 values('2');
commit;



create or replace function junk( someParameter varchar2 ) return sys_refcursor
is
      myCur      sys_refcursor;
      junkNumber number;
      maxRows number := 1;
begin

      --perform some data checks
      begin
            junkNumber := to_number(someParameter);

            exception
                  when others then
                        raise_application_error(-20101, 'Parameter not a number: ' || someParameter);
      end;

      --check for too many rows or NO rows
      begin
            select count(*) into junkNumber from tab1 where col1 = to_number(someParameter);

            case
                  when junkNumber = 0 then
                        raise_application_error(-20102, 'No rows found for: ' || someParameter);
                  when junkNumber > maxRows then
                        raise_application_error(-20103, 'Too many rows found for: ' || someParameter);
                  else
                        null;
            end case;
      end;

      open  myCur for 'select sysdate from tab1 where col1 = to_number(:x)' using someParameter;

      return myCur;

end;
/

show errors

-- no rows found
select junk('0') from dual;

-- too many rows found
select junk('2') from dual;

-- not a valid number
select junk('Y') from dual;

-- Just right.....
select junk('1') from dual;

 

by: mhci_nnePosted on 2007-03-14 at 01:50:31ID: 18716910

Is  raise_application_error(...) a build in PL/SQL function, if so then it should solve my problem right?

 

by: slightwvPosted on 2007-03-14 at 05:47:52ID: 18717837

Yes.  It's part of PL/SQL.

the code I posted should run as-is.

 

by: mhci_nnePosted on 2007-03-14 at 05:49:38ID: 18717850

Thanks that's all I needed :-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...