• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieiving OUT parameters & return value from a Sybase Stored Procedure

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!!
I am using JConnect52 JDBC driver to connect to Sybase Adaptive Server Anywhere 6.0.
My problem is this: I am unable to retrieve the OUT parameter from a Stored Procedure which returns multiple resultsets, a return value & an OUT parameter. Only the resultset & return value is retrieved. On attempting to retrieve the OUT parameter, i get an SQL Exception.
Consider a Sybase Stored Procedure :
procedure dba.MyProcedure(@SId integer,@VId integer,@outparam integer)
as
begin
select @outparam=48
select* from shippers where shippers.shipperid>@SId
select* from vineyards where vineyards.vineyardrid>@VId
return 678
end
This is the code i use to execute the above Stored Procedure & then retrieve the parameters:
// assuming i am already connected to sybase
String sStatement = new String("{?= call MyProcedure(?, ?, ?)}");
CallableStatement cs = conn.prepareCall(sStatement);

cs.setInt(2, 2);
cs.setInt(3, 2);
cs.setInt(4, 2);
cs.registerOutParameter (1, Types.INTEGER);
cs.registerOutParameter (4, Types.INTEGER);
System.out.println ("now executing the stored procedure... ");
if(!cs.execute())
{
System.out.println("Does not return a resultset");
}

while (cs.getMoreResults())
{
System.out.println ("retrieving a resultset.....");
ResultSet rs = cs.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();

int totalcols = rsmd.getColumnCount();
int firsttime = 1;
while(rs.next())
{
if(firsttime == 1)
{
for(int i =1;i<=totalcols;i++)
{
System.out.print(" "+rsmd.getColumnName(i));
}
firsttime = 0;
System.out.println("");
}

for(int i = 1; i<=totalcols;i++)
{
System.out.print(" "+rs.getString(i));
}
System.out.println("");

}
}

int iRetval = cs.getInt (1);
System.out.println ("Return Value = " + iRetval);

// retrieve the OUT params
int iOutparam = cs.getInt(4);
System.out.println ("OUT param = " + iOutparam);
The code execute fine upto printing the return value, but then an SQL Exception is thrown.
Is there some way that i can retrieve the parameters & return value & resultset?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//This is stored procedure
----------------------------
Stored procedure Name : MyProcedure
TABLENAME : EXTB
IN PARAMETERS : ALL PARAMETER EXCEPT D,E
OUT PARAMETER : D,E
D SEQUENCE NAME : DSEQ

CREATE OR REPLACE PROCEDURE MyProcedure(
A NUMBER,
B NUMBER,
C NUMBER,
D OUT NUMBER, //4,5 th filed out parameter
E OUT NUMBER) AS
BEGIN
SELECT DSEQ.NEXTVAL INTO D FROM DUAL;
SELECT ESEQ.NEXTVAL INTO E FROM DUAL;
INSERT INTO EXTB VALUES(A,B,C,D,E);
COMMIT;
END;

----------------------------
//This is java program
----------------------------
.........
......................
//in stored procedure 5 arguments ......that's I put 5 question //marks

String sStatement = new String("{call MyProcedure(?,?,?,?,?)}");
CallableStatement cs = conn.prepareCall(sStatement);
cs.setInt(1, 2);
cs.setInt(2, 2);
cs.setInt(3, 2);
cs.registerOutParameter (4, Types.INTEGER);//4,5 th fields getting from database
cs.registerOutParameter (5, Types.INTEGER);
int i=cstmt.executeUpdate();
if(i!=0)
{
...........throw the exception(error)........

}else{
// retrieve the OUT params
System.out.println("4 Th Value :"+ cs.getInt(4));
System.out.println("5 Th Value :"+ cs.getInt(5));

}
......
............
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic