Calling a Stored Procedure in a Database

This example demonstrates how to call stored procedures with IN, OUT, and IN/OUT parameters.
CallableStatement cs; try { // Call a procedure with no parameters cs = connection.prepareCall("{call myproc}"); cs.execute(); // Call a procedure with one IN parameter cs = connection.prepareCall("{call myprocin(?)}"); // Set the value for the IN parameter cs.setString(1, "a string"); // Execute the stored procedure cs.execute(); // Call a procedure with one OUT parameter cs = connection.prepareCall("{call myprocout(?)}"); // Register the type of the OUT parameter cs.registerOutParameter(1, Types.VARCHAR); // Execute the stored procedure and retrieve the OUT value cs.execute(); String outParam = cs.getString(1); // OUT parameter // Call a procedure with one IN/OUT parameter cs = connection.prepareCall("{call myprocinout(?)}"); // Register the type of the IN/OUT parameter cs.registerOutParameter(1, Types.VARCHAR); // Set the value for the IN/OUT parameter cs.setString(1, "a string"); // Execute the stored procedure and retrieve the IN/OUT value cs.execute(); outParam = cs.getString(1); // OUT parameter } catch (SQLException e) { }

Comments

5 Jan 2010 - 5:20am by ostermann.bernd@arcor.de (not verified)

Within a catch block: is another try/catch permissible?

6 Jan 2010 - 2:20pm by patrick

Yes, it is permissible and is a common pattern for database apps to clean up database objects after a database failure.

12 Feb 2010 - 8:15am by Victor (not verified)
Could you help me with this procedure? it has 4 in and 2 out parameters
PROCEDURE cmc01.consulta_saldos_x_dias ( p_centro_alta IN VARCHAR2, p_cuenta IN VARCHAR2, p_divisa IN VARCHAR2, p_cursor OUT PKG_CURSOR.DESA_CURSOR, p_error OUT VARCHAR2, p_dias IN NUMBER DEFAULT 90) ...
is it right if I call it this way? :
CallableStatement calls = conn.prepareCall("{call cmc01.consulta_saldos_x_dias(?,?,?,?)}"); calls.setString(1, "qwe"); calls.setString(2,"asd"); calls.setString(3,"zcx"); calls.setInt(4,90); calls.registerOutParameter(1, OracleTypes.OTHER, "CMC01.PKG_CURSOR.DESA_CURSOR"); //calls.registerOutParameter(1, OracleTypes.REF); calls.registerOutParameter(2,OracleTypes.VARCHAR); calls.execute();
oh, and here I describe the PKG_CURSOR.DESA_CURSOR:
PACKAGE cmc01.pkg_cursor IS TYPE desa_cursor IS REF CURSOR; ...
thanks in advance
12 Feb 2010 - 8:16am by Victor (not verified)

oh, my e-mail is vic.-w@hotmail.com

just in case

27 May 2010 - 12:26am by Anonymous (not verified)

If i want to call a piece of java code from a plsql procedure how can i do it??

18 Jun 2010 - 11:25pm by Anonymous (not verified)

very good

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.