Calling stored procedures with parameters in Java

Source: Internet
Author: User

JDBC Calls stored procedures: CallableStatement calls stored procedures in Java, which is quite fixed: Class.forName (...). Connection conn = Drivermanager.getconnection (..../***p is the name of the stored procedure to invoke, 4 parameters of the stored procedure, with 4? */callablestatement cstmt = Conn.preparecall ("{call P (?,?,?,?)}") in place of the rest of the code Tell JDBC, these parameters, which are the output parameters, the type of the output parameter is specified with Java.sql.Types * The following means, the 3rd one? And the 4th one? is the output parameter, the type is the *types of the INTEGER after the specific write what type, depends on how your stored procedure parameters defined */cstmt.registeroutparameter (3, Types.integer); Cstmt.registeroutparameter (4, Types.integer);/*** in my 1th? And the 2nd one? is the input parameter, the 3rd is the output parameter, 4th both input and output * The following is set their value, the first set to 3, the second set to 4, 4th set to 5* no 3rd, because it is the output parameter */cstmt.setint (1, 2); Cstmt.setint (4); Cstmt.setint (4, 5);//execute Cstmt.execute ();//Take the value of the 3rd parameter as an int type three = Cstmt.getint (3); System.out.println (three);//Take the value of the 4th parameter as an int. int four = Cstmt.getint (4); System.out.println (four);//Use up don't forget to shut up, after opening first off Cstmt.close (); Conn.close (); JDBC calls the stored procedure, which is enough to master this one. The following is the code for the stored procedure used by the above program, I use the Oracle database, but no matter what database, for your program, the JDBC end of the writing is the same. Create or replace Procedure P (v_a in Number,v_b Number,v_ret out number,v_temp on out number) isbeginif (V_a > V_b) Thenv_ret: = V_a;elsev_ret: = v_b;end if;v_temp: = v_temp + 1;end; 

  

Calling stored procedures with parameters in Java

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.