Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calling a stored procedure  RSS feed

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figuered it out.

Thank you
 
Bartender
Posts: 2633
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hanna,

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

Regards, Jan
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2633
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
Regards, Jan
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!