Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java, Web
Expertise: Intermediate
May 13, 2002

How to Create and Use CallableStatement Objects


A CallableStatement object provides a standard way for all RDBMS' to call stored procedures. A stored procedure is stored in a database; the call to the stored procedure is what a CallableStatement object contains. This call is written in an escape syntax that can take one of two forms: with a result parameter, and the other without one. A result parameter, a kind of OUT parameter, is the return value for the stored procedure. Both forms may have a variable number of parameters used for input (IN parameters), output (OUT parameters), or both (INOUT parameters). A question mark serves as a placeholder for a parameter.

Creating a CallableStatement object:
 
CallableStatement callStmt = con.prepareCall(
	"{call getData(?, ?)}");

The variable callStmt contains a call to the stored procedure getData, which has two input parameters and no result parameter. Whether the "?" placeholders are IN, OUT, or INOUT parameters depends on the stored procedure getTestData.

Suppose getTestData accepts two OUT parameters. Then you have to register the OUT parameter:
 
cstmt.registerOutParameter(1, java.sql.Types.TINYINT);
cstmt.registerOutParameter(2, java.sql.Types.DECIMAL, 3);

This is how to get it back:
 
byte x = cstmt.getByte(1);
Rajesh KS
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date