Java invoke procedures and functions for Oracle

Source: Internet
Author: User

/**


call a function in the database


a function is essentially a stored procedure that returns a result, and this example demonstrates how to invoke a function with the in, out, and in/out arguments


***********************************/


CallableStatement cs;


try {


//Call a function with no parameters; function returns a VARCHAR


//Preprocessing callable statement





cs = Connection.preparecall ("{? = Call MyFunc} ");





//Registration return value type


Cs.registeroutparameter (1, i);





//Execute and retrieve the returned value


Cs.execute ();


String RetValue = cs.getstring (1);





//Call function with an in parameter; The function returns a VARCHAR


cs = Connection.preparecall ("{? = Call Myfuncin (?)} ");





//Register The type of the return value


cs.registeroutparameter (1, Types.varchar);





Set the value for the in parameter


cs.setstring (2, "a string");





//Execute and retrieve the returned value


Cs.execute ();


RetValue = cs.getstring (1);





//Call function with an out parameter; The function returns a VARCHAR


cs = Connection.preparecall ("{? = Call Myfuncout (?)} ");





//Register The types of the return value and out parameter


cs.registeroutparameter (1, Types.varchar);


Cs.registeroutparameter (2, Types.varchar);





//Execute and retrieve the returned values


Cs.execute ();


RetValue = cs.getstring (1); return value


String outparam = cs.getstring (2); Out parameter





//Call function with a in/out parameter; The function returns a VARCHAR


CS = Connection.preparecall ("{? = Call Myfuncinout (?)} ");





//Register The types of the return value and out parameter


cs.registeroutparameter (1, Types.varchar);


Cs.registeroutparameter (2, Types.varchar);





//Set The value for the In/out parameter


cs.setstring (2, "a string");





//Execute and retrieve the returned values


Cs.execute ();


RetValue = cs.getstring (1); return value


Outparam = cs.getstring (2); In/out parameter


} catch (SQLException e) {


   }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.