Java: Calling a stored procedure in an oracle database

后端 未结 2 2016
南旧
南旧 2020-12-20 15:47

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

相关标签:
2条回答
  • 2020-12-20 15:59

    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.

    0 讨论(0)
  • 2020-12-20 16:17

    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!

    0 讨论(0)
提交回复
热议问题