File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and Relational Databases and the fly likes calling a stored procedure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "calling a stored procedure" Watch "calling a stored procedure" New topic
Author

calling a stored procedure

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hello all:

I have a stored procedure that returns a bigint not a resultset.

cs = connection.prepareCall(sql);
cs.execute();

How can I get the returned value?

thanks


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
I figuered it out.

Thank you
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2532
    
  10

Hanna,

Can you post your solution? It helps fellow ranchers that have the same question.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
Originally posted by Jan Cumps:
Hanna,

Can you post your solution? It helps fellow ranchers that have the same question.

Regards, Jan



Here is my solution:

Connection connection = null;
String sql = "{ ? = call getNextEntityId }";
Long id = null;
CallableStatement cs = null;
try
{
connection = session.connection();
cs = connection.prepareCall(sql);
cs.registerOutParameter(1, Types.BIGINT);
cs.execute();
id = cs.getLong(1);
logger.info("generate(): Id generated successfuly: " + id);

return(id);
}

The key is to register the OUT parameters before you execute the stored procedure. In the above case, the stored proc. returns one value, so I registered one value. Then, you get the registered values in the same order they are registerd and with the same type as they are registerd using cs.getXX()
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2532
    
  10

Thank you.
Regards, Jan
 
jQuery in Action, 2nd edition
 
subject: calling a stored procedure