简体   繁体   English

Java:在oracle数据库中调用存储过程

[英]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. 我正在尝试在oracle数据库中调用一个简单的存储过程。

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 我遇到麻烦的Java代码如下所示

    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. 当我在SQL Developer中运行该过程时,会看到结果。 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! 任何帮助,将不胜感激!

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! 执行查询后强制执行提交会导致数据库反映更改,因此问题中显示的代码是调用此类型存储过程的正确方法!

To be able to capture the return of procedure in Oracle database, try this. 为了能够捕获Oracle数据库中过程的返回,请尝试此操作。

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: 如果您以这种方式使用其他的Return Map SimpleJdbcCall:

    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 http://jameajudo.blogspot.com.br/2009/03/call-procedure-oracle-with-java-and.html

Tested on Oracle 10xe and 11xe. 在Oracle 10xe和11xe上进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM