JDBC Calls stored procedures
Steps:
1: Create an instance of the CallableStatement object through the Preparecall () method of the Connection object.
When you use the Preparecall () method of the Connection object, you need to pass in a string that is a string of type
This method indicates how the stored procedure is called.
{? = call <procedure-name>[(<ARG1>,<ARG2>,. ...)]}
{Call <procedure-name>[(<ARG1>,<ARG2>,. ...)]}
2: Register out parameters through the Registeroutparameter () method of the CallableStatement object;
3: Set in or out parameters through the Setxxx () method of the CallableStatement object;
If you want to set the parameter to NULL, you can use the SetNull () method
4: Execute the stored procedure through CallableStatement's Excute () method;
5: If you call a stored procedure with a return parameter, you also need to get its return value through the getxxx () of the CallableStatement object.
Case code:
/* * File name: Functiontest.java * Copyright: Copyrights by Www.huawei.com * Description: * Modified by: Cuigaochong * Modified: 2015-8-28 * Tracking Number: * Change the number: * Modify the inside Capacity: */package com.jdbc.function;import java.sql.callablestatement;import Java.sql.connection;import Java.sql.sqlexception;import java.sql.types;import Org.junit.test;import com.jdbc.cgc.pro.oracledbutils;/** * < A Word function Summary > < function description > * * @author name work number * @version [version number, 2015-8-28] * @see [Related class/method] * @since [Product/module version] */public clas S functiontest{private Oracledbutils utils = Oracledbutils.getinstance (); /** * < A sentence function description >jdbc call store Function < function details > * * @see [Class, Class # method, Class # Member] */@Test public void test00 () {Connection conn = null; CallableStatement callablestatement = null; String sql = "{? =call Get_sall (?,?)}"; try {conn = utils.connection (); 1: Create an instance of the CallableStatement object through the Preparecall () method of the Connection object, or, when using the Preparecall () method of the Connection object, you need to pass in a Strin Type Gstring,//This method indicates how the stored procedure is called. {? = call <procedure-name>[(<ARG1>,<ARG2>,. ...)]} {Call <procedure-name>[(<ARG1>,<ARG2>,. ...)]} CallableStatement = Conn.preparecall (sql); 2: Register out parameters through the Registeroutparameter () method of the CallableStatement object; Callablestatement.registeroutparameter (1, Types.NUM ERIC); Callablestatement.registeroutparameter (3, types.numeric); 3: Set the in or out parameter by the Setxxx () method of the CallableStatement object, or use the SetNull () method if you want to set the parameter to null Callablestatemen T.setint (2, 80); 4: Execute stored procedure through CallableStatement's Excute () method; Callablestatement.execute (); 5: If you call a stored procedure with a return parameter, you also need to get its return value through the getxxx () of the CallableStatement object; Double sum = callablestatement.getdouble (1); Long Empcount = Callablestatement.getlong (3); SYSTEM.OUT.PRINTLN (sum); System.out.println (Empcount); } catch (SQLException e) {e.printstacktrace (); } finally {Utils.releasesource (callablestatement, conn, null); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JDBC calls stored function stored procedure