• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access return value from stored procedure thru JDBC interface

 
Deepa Raghuraman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I want to know how to access return value from stored procedure through JDBC interface.I have used SQL server as database.If any one knows please reply soon.
Thank u
deepa

[This message has been edited by deepa raghuraman (edited November 26, 2001).]
[This message has been edited by deepa raghuraman (edited November 26, 2001).]
 
Sudha Lakshman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepa,
hope this helps you.
JDBC has a standard syntax for executing stored procedures, which takes one of two forms:
{call procedurename param1, param2, param3 ... }
{?= call procedurename param1, param2, param3 ... }
The parameters are optional. If your procedure doesn�t take any parameters, the call might look like this:
{call myprocedure}
If your stored procedure returns a value, use the form that starts with ?=. You can also use ? for any of
the parameter values in the stored procedure call and set them just like you set parameters in a
PreparedStatement. In fact, the CallableStatement interface extends the PreparedStatement
interface.
Some stored procedures have a notion of "out parameters," in which you pass a parameter in, the
procedure changes the value of the parameter, and you need to examine the new value. If you need to
retrieve the value of a parameter, you must tell the CallableStatement interface ahead of time by
calling registerOutParameter:
public void registerOutParameter(int whichParameter, int sqlType)
public void registerOutParameter(int whichParameter, int sqlType,
int scale)
public void registerOutParameter(int whichParameter, int sqlType,
String typeName)
After you execute your stored procedure, you can retrieve the values of the out parameters by calling
one of the many get methods. As with the set methods, the parameter numbers on the get methods are
numbered starting from 1 and not 0. For example, suppose you have a stored procedure called
findPopularName that searches the Person table for the most popular first name. Suppose, furthermore,
that the stored procedure has a single out parameter that is the most popular name. You invoke the
procedure this way:
CallableStatement cstmt = myConnection.prepareCall(
"{call findPopularName ?}");
cstmt.registerOutParameter(1, Types.VARCHAR);
cstmt.execute();
System.out.println("The most popular name is "+
cstmt.getString(1));
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic