How to call a function or stored procedure stored in a database using JDBC:
* 1. Create a callablestatement with the Preparecall () method of the Connection object
* An instance of an object that, when using the Preparecall () method of the Connection object, needs to pass in a string of type
* This string is used to indicate how the stored procedure is called
* 2. Register out parameters with the Registeroutparameter () method of the CallableStatement object
* 3. Set in or out by the Setxxx () method of the CallableStatement object
* parameter, if you want to set the parameter to NULL, you can use SetNull ()
* 4. Execute the stored procedure through the Execute () method of the CallableStatement object
* 5. If you are calling a stored procedure with a return parameter, you do not have to get it through the getxxx () function of the CallableStatement object.
The specific code implementation:
1 @Test2 Public voidtestcallablestatement () {3Connection Connection =NULL;4 /**5 * Call storage function 1. {? = call <procedure-name>[(<ARG1>,<ARG2>,. ...)]} 6 * Call stored procedure 2. {Call <procedure-name>[(<ARG1>,<ARG2>,. ...)]}7 */8 //the difference between calling a stored function and calling a stored procedure, a SQL statement9String sql = "{=" = Call <procedure-name>[(<ARG1>,<ARG2>,. ...)]} ";TenCallableStatement CallableStatement =NULL; One Try { A -Connection =jdbctools.getconnection (); - /* the * 1. Create a callablestatement with the Preparecall () method of the Connection object - * An instance of an object that, when using the Preparecall () method of the Connection object, needs to pass in a string of type - * This string is used to indicate how the stored procedure is called - */ +CallableStatement =connection.preparecall (SQL); - + /* A * 2. Register out parameters with the Registeroutparameter () method of the CallableStatement object at */ -Callablestatement.registeroutparameter (1, types.numeric); -Callablestatement.registeroutparameter (3, types.numeric); - - /* - * 3. Set in or out by the Setxxx () method of the CallableStatement object in * parameter, if you want to set the parameter to NULL, you can use SetNull () - */ toCallablestatement.setint (2, 80); + - /*4. Execute the stored procedure through the Execute () method of the CallableStatement object*/ the Callablestatement.execute (); * $ /*Panax Notoginseng * 5. If you are calling a stored procedure with a return parameter, you do not need to pass the CallableStatement object's getxxx () - */ the DoubleSumsalary = callablestatement.getdouble (1); + LongEmpcount = Callablestatement.getlong (3); A}Catch(Exception e) { the e.printstacktrace (); +}finally { -Jdbctools.release (NULL, callablestatement, connection); $ } $}
The difference between calling the function and the SQL statement of the stored procedure:
* Call storage function 1. {? = call <procedure-name>[(<ARG1>,<ARG2>,. ...)]}
* Call stored Procedure 2. {Call <procedure-name>[(<ARG1>,<ARG2>,. ...)]}
This knowledge point temporarily useless, first make notes, later use later to further study, JDBC Learning temporarily, Spring, I come!
Personal sentiment: The simple things to achieve the ultimate, playing a solid foundation, write excellent code
This article for Bo Master original article, reprint please indicate source: http://www.cnblogs.com/ysw-go/
1, the original original article of the blog, are I usually learn to do the notes, if there are errors, please correct me.
2, if there is infringement of your intellectual property rights and copyright issues, please inform me, I will immediately make the processing of the article.
3, the purpose of this blog is to use the knowledge exchange, reproduced from other blogs or websites, as their reference, thank the original staff of these articles
JDBC Learning Notes (10)--calling functions & stored Procedures