JDBC Passing ResultSet to a Stored Procedure in Oracle through Java

I have following Stored Procedure written in Oracle.
create or replace package types
as
     TYPE NumCurType IS REF CURSOR;
     FUNCTION add_numbers(c2 IN NumCurType)
     RETURN NUMBER;
END;
CREATE OR REPLACE PACKAGE BODY types
as
     FUNCTION add_numbers(c2 IN NumCurType)
     RETURN NUMBER
     IS
     v_sum number :=0;
     num_record num%rowtype;
     begin
     loop
     fetch c2 into num_record;
     v_sum:= v_sum + num_record.id ;
     dbms_output.put_line(v_sum);
     exit when c2%NOTFOUND;
     end loop;
     close c2;     
     return v_sum;
     end add_numbers;
begin
dbms_output.put_line('test');
end;
And the following java class to call the stored procedure.
import java.io.*;
import java.sql.*;
class test
     public static void main(String arg[]) throws Exception
          Class.forName("oracle.jdbc.driver.OracleDriver");
          Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:oracle","scott","tiger");
          Statement stmt = conn.createStatement();
          String sql = "select id from num";
          ResultSet rs = stmt.executeQuery(sql);
          System.out.println(rs);
          if(rs != null)
               rs.next();
          CallableStatement cstmt = conn.prepareCall("{ ? = call types.add_numbers(?) }");
          cstmt.registerOutParameter(1,java.sql.Types.DOUBLE);     
     cstmt.setObject(2,rs,OracleTypes.CURSOR);                    
          cstmt.executeUpdate();
          System.out.println(cstmt.getObject(1));
          System.out.println("Procedure Exceuted");
When I run the class I get following error
java.sql.SQLException end of TNSData channel
Can anybody help me in locating the error?

Hi Jain,
As far as I know, what you are attempting to do cannot be done. If I remember correctly, this question has been previously discussed on Oracle's "Technet" forums:
http://technet.oracle.com
There is no capability in the Oracle JDBC driver for translating a "ResultSet" to a REF CURSOR.
You may also be able to find more details at the following Web sites:
http://asktom.oracle.com
http://metalink.oracle.com
Good Luck,
Avi.

Similar Messages

  • Calling Stored Procedure from Oracle DataBase using Sender JDBC (JDBC-JMS)

    Hi All,
    We have requirement to move the data from Database to Queue (Interface Flow: JDBC -> JMS).
    Database is Oracle.
    *Based on Event, data will be triggered into two tables: XX & YY. This event occurs twice daily.
    Take one field: 'aa' in XX and compare it with the field: 'pp' in YY.
    If both are equal, then
         if the field: 'qq' in YY table equals to "Add" then take the data from the view table: 'Add_View'.
         else  if the field: 'qq' in YY table equals to "Modify"  then take the data from the view table: 'Modify_View'.
    Finally, We need to archive the selected data from the respective view table.*
    From each table, data will come differently, means with different field names.
    I thought of call Stored Procedure from Sender JDBC Adapter for the above requirement.
    But I heard that, we cannot call stored procedure in Oracle through Sender JDBC as it returns Cursor instead of ResultSet.
    Is there any way other than Stored Procedure?
    How to handle Data Types as data is coming from two different tables?
    Can we create one data type for two tables?
    Is BPM required for this to collect data from two different tables?
    Can somebody guide me on how to handle this?
    Waiting eagerly for help which will be rewarded.
    Thanks and Regards,
    Jyothirmayi.

    Hi Gopal,
    Thank you for your reply.
    >Is there any way other than Stored Procedure?
    Can you try configuring sender adapter to poll the data in intervals. You can configure Automatic TIme planning (ATP) in the sender jdbc channel.
    I need to select the data from different tables based on some conditions. Let me simplify that.
    Suppose Table1 contains 'n' no of rows. For each row, I need to test two conditions where only one condition will be satisfied. If 1st condition is satisfied, then data needs to be taken from Table2 else data needs to be taken from Table3.
    How can we meet this by configuring sender adapter with ATP?
    ================================================================================================
    >How to handle Data Types as data is coming from two different tables?
    If you use join query in the select statement field of the channel then whatever you need select fields will be returned. This might be fields of two tables. your datatype fields are combination of two diff table.
    we need to take data only from one table at a time. It is not join of two tables.
    ================================================================================================
    Thanks,
    Jyothirmayi.

  • Resultset from a Stored Procedure

    Hello Everyone,
    Is it possible to return a resultset from a stored procedure? I need to do display set of rows which are resulted by joining few tables.
    Is it possible?
    Please help..
    TIA
    Regards,
    Rao Santapur.
    null

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Raymond Tang ([email protected]):
    You can use 'REF CURSOR'<HR></BLOCKQUOTE>
    I have created a stored procedure with a ref cursor- what parameters do i need to pass via Sql*Plus to get results?
    [email protected]
    null

  • How to pass parameter from 1 stored procedure to another stored procedure inside crystal report

    Hi
    I have several stored procedure in my Crystal Report. I am wondering if it is possible for me to pass a parameter to one of the stored procedure and to use the result of that stored procedure E.g. CustomerCode. To another 2 stored procedure to generate the report dynamically?
    I have 3 stored procedure
    The 1st one is used to gather information and process the calculation
    another 2 stored procedure is used for generate the graph and both of them required to take 2 parameters. The 1st stored procedure will require 1 parameter (E.G. Reference Code) and will return a set of information including the data that could be use on the other 2 stored procedures.
    After I added these 2 stored procedure, it requires me to pass 3 parameters to the report. I would like to know if I could only pass the Reference Code for stored procedure 1 and use it to retrieve the information for the other 2 parameter?
    Thanks in advance
    Chi

    Hi Chi
    To pass parameter from 1 stored procedure to another stored procedure, you will have to create sub report. In your case you will have to create 2 sub reports for 2nd and 3rd stored procedure and link those sub reports with the main report using Reference Code field in order to pass the values.
    After creating the report when you will refresh the report, it will ask 4 parameters, one parameter for main report, one for the first subreport and two for second subreport to fetch the data correctly.
    Regards
    Poonam Thorat.

  • How to return a resultset in a stored procedure without using Cursor

    Dear all,
    I need to return a resultset in a stored procedure.
    I know I can return a Cursor.
    But because there are many complicate logics I need to carry out,
    so I can finish all the logic in a SINGLE SQL select statement, so I think it can't
    use Cursor as return value.
    Does anybody know other approaches? Please help.
    Thanks!

    Some basic techniques that one can use in SQL. These are merely to illustrate (simplistically) different approaches.
    > field3 = select count(*) from table2,table1 where table2.id = table1.id
    This can be done via an analytical function (refer to the SQL Reference) or very simply, as an in-line select as follows:
    SQL> select
    2 o.object_name,
    3 (select count(*) from user_objects u where u.object_type = o.object_type) as "ONE OF",
    4 o.object_type
    5 from user_objects o
    6 where o.object_name = 'LISTFILES';
    OBJECT_NAME ONE OF OBJECT_TYPE
    LISTFILES 8 PROCEDURE
    So LISTFILE is "one of 8" procedures. I would however rather so this via an analytic function as these are a lot more flexible. Note the WITH clause that allows "modularisation" of SQL - kind of like creating "sub-SQL results" in a single SQL statement:
    SQL> with DATASET1 as(
    2 select
    3 object_name,
    4 count(distinct object_name) over (partition by object_type) as TOTAL_TYPES,
    5 object_type
    6 from user_objects
    7 ),
    8 DATASET2 as(
    9 select
    10 name,
    11 text as SOURCE_LINE,
    12 line as LINE_NO,
    13 count(line) over (partition by name) as TOTAL_LINES
    14 from user_source
    15 )
    16 select
    17 object_name,
    18 '1 of '||total_types||' '||object_type as "TYPE",
    19 source_line as "1st LINE",
    20 total_lines as "TOTAL LINES"
    21 from DATASET1 ds1,
    22 DATASET2 ds2
    23 where ds1.object_name = ds2.name
    24 and ds1.object_name = 'LISTFILES'
    25* and ds2.line_no = 1
    SQL> /
    OBJECT_NAME TYPE 1st LINE TOTAL LINES
    LISTFILES 1 of 8 PROCEDURE procedure ListFiles( cDirectory in varchar2 ) 3
    SQL>
    SQL is quite powerful and analytical functions allows all kinds of aggregation processing per row, accessing the leading or lagging rows' data, etc.
    You need to play around with this to get to grips with it and how to apply it.

  • Calling Stored Procedure with Paramters in Java

    Hi
    I havent done alot of sql/java work so I'm wondering if I could get some help.
    I have the java code
    private void processPatient(Element pat)
    throws PatientException,
    ParseException,
    SQLException,
    Exception {
    Connection con = null;
    ResultSet rs;
    CallableStatement cs;
    ...<code>
    cs = con.prepareCall("{call updt_pat.set_pat_ppe}");
    rs = cs.executeQuery();
    Does this look correct? The proc updt_pat.set_pat_ppe also takes an int parameter...how would I modify this to accept the paramter? Like
    cs = con.prepareCall("{call updt_pat.set_pat_ppe paramter}");
    Thanks for the help!!
    Mani

    The actual implementation of what the setInt does and how it does it is up the JDBC driver vendor. Basically its function is as follows (implementation will be driver specific) it takes the int value and passes it to the Stored Procedure being called as the (in this case) 1st input parameter expected by the stored procedure. Whatever the name of the IN variable is within the stored procedure is irrelevant to the JDBC code, basically it will be assigned to the name of the first IN parameter within the Stored Procedure.
    The error you are reporting:
    xxx.java:272: cannot resolve symbol
    symbol : method setInt (java.lang.Integer,int)
    location: interface java.sql.CallableStatement
    cs.setInt(1, ppath.getPatId());
    ^
    1 error
    is interesting as it is claiming that the setInt method requires as its first argument an Integer object - this is not what the specification requires, it just wants an index number as a Java int:
    setInt(int index, int value), note no java.lang.Integer
    maybe this is a driver specific implementation?
    Try the following (not in any specific order):
    1 - int value = ppath.getPatId();
    cs.setInt(1, value);
    2 - Integer index = new Integer(1);
    cs.setInt(index , ppath.getPatId());
    3 - int = ppath.getPatId();
    Integer index = new Integer(1);
    cs.setInt(index , value);
    Let me know if this solves your problem.

  • SQLException while calling a Stored Procedure in Oracle

    Hi all,
    I am getting this error while calling a Stored Procedure in Oracle...
    java.sql.SQLException: ORA-00600: internal error code, arguments: [12259], [], [
    at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:207)
    at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:540)
    at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1273)
    at oracle.jdbc.ttc7.TTC7Protocol.fetch(TTC7Protocol.java:780)
    at oracle.jdbc.driver.OracleResultSet.next(OracleResultSet.java:135)
    at StoredProcedureDemo.main(StoredProcedureDemo.java:36)
    The Program is ...
    import java.sql.*;
    public class StoredProcedureDemo {
         public static void main(String[] args) throws Exception {
              Connection con = null;
              ResultSet rs = null;
              Statement st = null;
              CallableStatement cs = null;
              int i;
              try {
                   Class.forName("oracle.jdbc.driver.OracleDriver");
                   con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:SHYAM","scott","tiger");
                   System.out.println("Got Connection ");
                   st = con.createStatement();
                   String createProcedure = "create or replace PROCEDURE Get_emp_names (Dept_num IN NUMBER) IS"
                             +" Emp_name VARCHAR2(10);"
                             +" CURSOR c1 (Depno NUMBER) IS"
                             +" SELECT Ename FROM emp WHERE deptno = Depno;"
                             +" BEGIN"
                             +" OPEN c1(Dept_num);"
                             +" LOOP"
                             +" FETCH c1 INTO Emp_name;"
                             +" EXIT WHEN C1%NOTFOUND;"
                             +" END LOOP;"
                             +" CLOSE c1;"
                             +" END;";
                   System.out.println("Stored Procedure is \n"+createProcedure);
                   i = st.executeUpdate(createProcedure);
                   System.out.println("After creating the Stored Procedure "+i);
                   cs = con.prepareCall("{call Get_emp_names(?)}");
                   System.out.println("After calling the Stored Procedure ");
                   cs.setInt(1,20);
                   System.out.println("Before executing the Stored Procedure ");
                   rs = cs.executeQuery();
                   System.out.println("The Enames of the given Dept are ....");
                   while(rs.next()) {
                        System.out.println("In The while loop ");
                        System.out.println(rs.getString(1));
              catch (Exception e) {
                   e.printStackTrace();
    Stored Procedure is ...
    create or replace PROCEDURE Get_emp_names (Dept_num IN NUMBER) IS
    Emp_name VARCHAR2(10);
    CURSOR c1 (Depno NUMBER) IS
    SELECT Ename FROM emp WHERE deptno = Depno;
    BEGIN
    OPEN c1(Dept_num);
    LOOP
    FETCH c1 INTO Emp_name;
    EXIT WHEN C1%NOTFOUND;
    END LOOP;
    CLOSE c1;
    END;
    Stored procedure is working properly on sql*plus(Oracle 8.1.5)) editor. But it is not working from a standalone java application. Can anyone please give me a solution.
    thanks and regards
    Shyam Krishna

    The first solution is to not do that in java in the first place.
    DDL should be in script files which are applied to oracle outside of java.
    Other than I believe there are some existing stored procedures in Oracle that take DDL strings and process them. Your user has to have permission of course. You can track them down via the documentation.

  • Need sample source code for calling stored procedure in Oracle

    Hi.
    I try to call stored procedure in oracle using JCA JDBC.
    Anybody have sample source code for that ?
    Regards, Arnold.

    Thank you very much for a very quick reply. It worked, but I have an extended problem for which I would like to have a solution. Thank you very much in advance for your help. The problem is described below.
    I have the Procedure defined as below in the SFCS1 package body
    Procedure Company_Selection(O_Cursor IN OUT T_Cursor)
    BEGIN
    Open O_Cursor FOR
    SELECT CompanyId, CompanyName
    FROM Company
    WHERE CompanyProvince IN ('AL','AK');
    END Company_Selection;
    In the Oracle Forms, I have a datablock based on the above stored procedure. When I execute the form and from the menu if I click on Execute Query the data block gets filled up with data (The datablock is configured to display 10 items as a tabular form).
    At this point in time, I want to automate the process of displaying the data, hence I created a button and from there I want to call this stored procedure. So, in the button trigger I have the following statements
    DECLARE
    A SFCS1.T_Cursor;
    BEGIN
    SFCS1.Company_Selection(A);
    go_Block ('Block36');
    The cursor goes to the corresponding block, but does not display any data. Can you tell me how to get the data displayed. In the future versions, I'm planning to put variables in the WHERE clause.

  • Importing function with multiple ref cursors in Stored Procedure of Oracle 12c database Using EF6

    Hi Good day!
    I can able to import function for stored procedure of oracle db and able to add the complex type and get the output but i tried to import the procedure which having two ref cursors and unable to retrieve the column information. Only able to retrieve the
    columns of first ref cursor.  Please help me to get the result of two ref cursors which acting as out parameters.

    Having to ref cursors return mutiple recordsets in an Oracle package is like haveng two resultsets return from a MS SQL Server sparc.
    The link may point you in the right direction.
    http://www.codeproject.com/Articles/675933/Returning-Multiple-Result-Sets-from-an-Entity-Fram

  • How to call a sql server stored procedure from oracle

    Hi all,
    Please anybody tell me how to call a sql server stored procedure from oracle.
    I've made an hsodbc connection and i can do insert, update, fetch data in sql server from oracle. But calling SP gives error. when I tried an SP at oracle that has line like
    "dbo"."CreateReceipt"@hsa
    where CreateReceipt is the SP of sql server and hsa is the DSN, it gives the error that "dbo"."CreateReceipt" should be declared.
    my database version is 10g
    Please help me how can i call it... I need to pass some parameters too to the SP
    thanking you

    hi,
    thank you for the response.
    when i call the sp using DBMS_HS_PASSTHROUGH, without parameters it works successfully, but with parameters it gives the following error
    ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
    [Generic Connectivity Using ODBC][Microsoft][ODBC SQL Server Driver]Invalid parameter number[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index (SQL State: S1093; SQL Code: 0)
    my code is,
    declare
    c INTEGER;
    nr INTEGER;
    begin
    c := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@hsa;
    DBMS_HS_PASSTHROUGH.PARSE@hsa(c, 'Create_Receipt(?,?)');
    DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsa(c,1,'abc');
    DBMS_HS_PASSTHROUGH.BIND_VARIABLE@hsa(c,2,'xyz');
    nr:=DBMS_HS_PASSTHROUGH.EXECUTE_NON_QUERY@hsa(c);
    DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@hsa(c);
    end;
    Create_Receipt is the sp which requires two parameters.
    please give me a solution
    thanking you
    sreejith

  • How to use a stored procedure in oracle reports

    How to use a stored procedure in oracle reports

    Dear,
    In report triggers you can write your procedure/functions or call it like
    function AfterPForm return boolean is
    begin
    myprocedure(:mydate);
    return (TRUE);
    end;
    Thanks
    Jamil

  • Calling stored procedure in Oracle forms

    I have a stored procedure in Oracle which is declared as follows in the package
    SFCS1.Company_Selection(O_Cursor IN OUT T_Cursor, cls IN Varchar2);
    Where T_Cursor is defined as a Ref Cursor
    From the Oracle forms I'm using the following code
    SFCS1.Company_Selection(A,my_cls);
    go_block('Block50');
    Execute_Query;
    I get the error message "FRM40505:Oracle Error: Unable to Perform Query". If I hardcode the value of my_cls in the query it runs properly. Any solutions will be really helpful
    Further to this, I want to put the single quotes around a value (for eg. 'A') from a variable. For instance I'm getting a value from my_cls and for the output I want to surround it with the single quotes, can somebody tell me how to do it.
    Thanks in advance

    This is a bit of a roundabout way to do it? Try setting up the block data source as procedure and set the values in the property palette of the data block.
    e.g.
    Query Data Source Type = Procedure
    Query Data Source Name = SFCS1.Company_Selection
    Query Data Source Columns = (Whatever columns/items you have in your datablock)
    Query Data Source Arguments = Argument names are your ref cursor and your variable.
    Check out basing data blocks on Ref Cursors.
    HTHs
    L :-)

  • How to verify stored procedures in Oracle 10g.

    I would like to locate default stored procedure in Oracle 10g database.
    And Suggest which stored procedure can be converted in to JAVA code ??

    If the Java part of the question refers to looking for potential to speed up your stored procedures:
    You should consider native compilation which transfers your code to shared libraries which are bound to the DB kernel.
    In this case however consider, that this does not make any sense/improvement for pure data access statements but only for procedures consisting of complex algorithmic processing.
    For details refer to
    [How native compilation works|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/tuning.htm#sthref2280]
    and
    [Setting Up and Testing PL/SQL Native Compilation|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/tuning.htm#sthref2309]

  • How to write a PL/SQL stored procedure in Oracle to call Webservice

    Can any one pelase send me a code on how to write a PL/SQL stored procedure in Oracle database to call the Webservice ?
    Thanks,
    Rajesh

    Were you able to solve this problem

  • How to Create a Stored Procedure in Oracle

    I try to create a very simple Stored Procedure from Oracle SQL Developer, got the following error. Can someone give me some help on this? I am new on this. Thanks.
    Error(4,1): PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: := ( ; not null range default character The symbol ";" was substituted for "BEGIN" to continue.
    create or replace PROCEDURE Test
    AS ACCESSTYP_ID ACCESSTYP.ACCESSTYPCD%TYPE
    BEGIN
         SELECT ACCESSTYPCD
         INTO ACCESSTYP_ID
         FROM ACCESSTYP
         WHERE ACCESSTYPCD = 'WWW';
    END;

    I found out I forgot to put ";" after the declare.
    How do I test it call this stored procedure from
    Oracle SQL Developer.
    create or replace PROCEDURE Test_VL
    AS ACCESSTYP_ID
    ACCESSTYP.ACCESSTYPCD%TYPE;
         SELECT ACCESSTYPCD
         INTO ACCESSTYP_ID
         FROM ACCESSTYP
         WHERE ACCESSTYPCD = 'WWW';
    END;in your SQL Developer window just enclosed your procedure with a Begin ... End.
      Begin
        Test_VL;
      End;

Maybe you are looking for

  • ITunes wont find my music on my external drive (when it used to!)

    I recently filled my PC's interntal harddrive with music to the point where it was so slow I had to to buy an external drive to keep it on. I tranferred all my music on to the hard drive and have had no problems with it until today. Today, I started

  • Why epson projectors work perfectly in MacOS X Lion and not in Windows 7 with bootcamp?

    Hi, Irecently bought the new Macbook Air 2011, which came with the Intel Graphics HD3000 video card, as many of you, I was forced to install windows using bootcamp software, everything worked fine, till I tried to connect it to theprojector in office

  • Pioneer DVR 111-D

    I made a mistake and ordered a "111" instead of a "110." Does anybody know if a "111" is compatible with a Mac G4?

  • 3.1EA1: Completion insight

    Hello, Im using SQL Developer on Windows 7 32-bit, Java 1.6.0_27, Oracle 10gR2 Completion insight is not working when writing a query in an *.sql file. Eventough the database connection is opened and selected in the sql-worksheet. Kind regards, D.

  • TableView Sorting issues

    Hi, Just a short question about sorting.  In one of our tables, there are entries for a column that have names starting With and without capital letters.  Now, is there a way to force the sort to not care whether or not the first word is Capitalized