aeros
aeros

Reputation: 314

Java: Calling a stored procedure in an oracle database

EDIT: While some of the answers in this question may help others with different problems, the solution was actually related to some bug with the auto-commit feature on a database connection! Forcing a commit after executing the query caused the database to reflect the changes, thus the code shown below IS the correct way to call a stored procedure of this type

I'm trying to call a simple stored procedure in an oracle database.

The procedure looks like this:

procedure clear_orderProcDtlByOrdId(p_order_id in order_header.order_id%type,
                                    p_transaction_id in sl_order_processing_dtl.transaction_id%type DEFAULT NULL,
                                    p_item_action_id in sl_order_processing_dtl.item_action_id%type DEFAULT NULL )
...

The java code I'm having trouble with looks like this

    try 
    {
        CallableStatement storedProc = conn.prepareCall("{call PKG_PI_FRAUD.clear_orderProcDtlByOrdId(?)}");
        storedProc.setString(1, orderID);
        storedProc.execute();
    } 
    catch (SQLException e) 
    {
        e.printStackTrace();
    }

I'm not receiving any errors at all, however there are no database changes being reflected. When I run the procedure in SQL Developer I see results. I thought it might be because of a commit issue, but the connection I have established is in auto-commit mode.

Any help would be appreciated!

Upvotes: 9

Views: 56459

Answers (2)

aeros
aeros

Reputation: 314

While some of the answers in this question may help others with different problems, the solution was actually related to some bug with the auto-commit feature on a database connection! Forcing a commit after executing the query caused the database to reflect the changes, thus the code shown in the question IS the correct way to call a stored procedure of this type!

Upvotes: 3

SVerussa
SVerussa

Reputation: 49

To be able to capture the return of procedure in Oracle database, try this.

public static void main(String[] args) {

        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:xe";
            Connection con = DriverManager.getConnection(url, db_user, password);
            System.out.println("Connected to database");

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date now = new java.sql.Date(simpleDateFormat.parse("12/02/2001").getTime());

            String command = "{call SALDOS(?,?)}";
            CallableStatement cstmt = con.prepareCall(command);
            cstmt.registerOutParameter(2, Types.DECIMAL);

            cstmt.setDate(1, now);
            cstmt.execute();
            Double str = cstmt.getDouble(2);

            cstmt.close();
            System.out.println("Retorno: " + str);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

If you use a different Returns Map SimpleJdbcCall this way:

    SimpleJdbcCall call = Util.getSimpleJdbcCallInstance();
    call.setProcedureName("PROCED_CONDOMINIAL");
    call.declareParameters(
            new SqlParameter("CONDOMINIO", Types.VARCHAR),
            new SqlParameter("BLOCO", Types.VARCHAR),,
            new SqlOutParameter("P_NUMERO", Types.NUMERIC),
            new SqlOutParameter("P_LOG", Types.VARCHAR));

    Map<String, Object> parametros = new HashMap<String, Object>();
    parametros.put("CONDOMINIO_IC", descricaoCondominio);
    parametros.put("BLOCO_IC", imovelCondominial.getBloco());

    Map<String, Object> out = call.execute(parametros);
    BigDecimal chave = (BigDecimal) out.get("P_NUMERO");
    imovelCondominial.setId(chave.longValue());

and the declaration of the procedure

create or replace PROCEDURE         PROCED_CONDOMINIAL
               (CONDOMINIO            VARCHAR2,
                BLOCO                 VARCHAR2,
                NUMERO                OUT NUMBER,
                LOG                   OUT VARCHAR2)      -- PARAMETROS DE SAIDAS (OUT).-

Worked here. Look at this blog.

http://jameajudo.blogspot.com.br/2009/03/call-procedure-oracle-with-java-and.html

Tested on Oracle 10xe and 11xe.

Upvotes: 1

Related Questions