JDBC calls stored function stored procedure

Source: Internet
Author: User

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>[(&LT;ARG1&GT;,&LT;ARG2&GT;,. ...)]}
{Call <procedure-name>[(&LT;ARG1&GT;,&LT;ARG2&GT;,. ...)]}

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>[(&LT;ARG1&GT;,&LT;ARG2&GT;,. ...)]}                        {Call <procedure-name>[(&LT;ARG1&GT;,&LT;ARG2&GT;,. ...)]}                        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

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.