Calling stored procedures or functions in Java

Source: Internet
Author: User

1. Call the CallableStatement cs = con. prepareCall ("{? = Call get_pname (?,?,?)} "); First? Indicates the returned value, followed? It can be an input parameter or an output parameter. First? Is the return parameter, so there must be a statement following: connection. registerOutParameter (1, Types. VARCHAR); (Types. varchar is of type? The registerOutParameter statement: connection. registerOutParameter (2, Types. VARCHAR); (2 is the 2nd placeholder, Types. varchar is of the type) the final output result is: System. out. println (cs. getString (1); (1 is the corresponding output parameter, the first output parameter) 2. call the Stored Procedure CallableStatement cs = con. prepareCall ("{call stu_pro (?,?,?)} "); (The difference with a function is: No? =) To print the value obtained from the call process in a java program, you must call the stored procedure with output parameters. The usage is the same as that of the called function. 3. simple example [java] package com. dgy. app; import java. SQL. callableStatement; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. types; public class OraclePro {/*** connect to database */public static Connection getConnection () {Connection con = null; try {Class. forName ("oracle. jdbc. driver. oracleDriver "); String url =" jdbc: oracle: thin: @ PC-200911181406: 1521: dgy"; String user = "dwj"; String pwd = "dwj"; con = DriverManager. getConnection (url, user, pwd);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();} return con;} public static void main (String [] args) throws SQLException {// System. out. println (OraclePro. getConnection (); Connection con = OraclePro. getConnection (); CallableStatement cs = con. prepareCa Ll ("{? = Call get_pname} "); // function cs without parameters. registerOutParameter (1, Types. VARCHAR); // The first placeholder is output, and the type is varchar cs.exe cute (); // The execute () System cannot be forgotten. out. println (cs. getString (1); // print the output result, corresponding to registerOutParameter} [java] // The CallableStatement cs = con function with one input parameter. prepareCall ("{? = Call get_pname1 (?)} "); Cs. registerOutParameter (1, Types. VARCHAR); cs. setLong (); // The input parameter is 25 cs.exe cute (); // The CallableStatement cs = con function with one output parameter. prepareCall ("{? = Call get_pname2 (?)} "); Cs. registerOutParameter (1, Types. VARCHAR); cs. registerOutParameter (2, Types. VARCHAR); cs.exe cute (); www.2cto. comSystem. out. println (cs. getString (1); System. out. println (cs. getString (2); // the above two results are the same, because the two results mean the same // one input parameter and one output parameter CallableStatement cs = con. prepareCall ("{? = Call get_pname3 (?,?)} "); // 1st are returned values, 2nd are input parameters, and 3rd are Output Parameters cs. registerOutParameter (1, Types. VARCHAR); cs. setLong (2, 25); cs. registerOutParameter (3, Types. VARCHAR); cs.exe cute (); System. out. println (cs. getString (1); System. out. println (cs. getString (3); the method used to call a stored procedure is the same as that used to call a function. It is just con. prepareCall ("{call procedure (?,?)} ") No? =.

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.