Retrieiving OUT parameters & return value from a Sybase Stored Procedure
Bhavin Shah
Greenhorn
Joined: Dec 10, 2000
Posts: 7
posted
0
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?
DAYANAND BURAMSHETTY
Ranch Hand
Joined: Aug 06, 2001
Posts: 34
posted
0
//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));
} ...... ............
Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Retrieiving OUT parameters & return value from a Sybase Stored Procedure