首页 > 解决方案 > JDBC - 从 JAVA 调用 PLSQL 会产生 Java.sql.SQLException: ORA-06550

问题描述

我在从 Java 调用 PLSQL 时遇到了很大的麻烦。这是我的代码:

static final String PLSQL = "{call DBK_PDG_METADATI_CEDOLINO.dbp_main(?,?,?,?,?,?,?,?,?)}";

Connection conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());
CallableStatement cs = conn.prepareCall(PLSQL);

    for (Cedolino item : items) {
        LOG.info("################# ELABORAZIONE CEDOLINO " + item.getTestata().getAnagrafica().getCodFiscaleAmministrato() + " #################");
        cedolini.getCedolino().add(item);
        setParametersForPlSql(cs, item);

        try{
            cs.execute();
        }catch(SQLException e){
            LOG.info(e.toString());
        }

    }

cs.close();
conn.close();

private void setParametersForPlSql(CallableStatement cs, Cedolino ced){

    try {
        cs.setInt("tipo_lancio", 1);
        cs.setString("iscr", ced.getTestata().getTrattamento().getIscrizione().trim());
        cs.setString("rts", ced.getTestata().getDpt().trim());
        cs.setString("codfisc", ced.getTestata().getAnagrafica().getCodFiscaleAmministrato().trim());
        cs.setString("lingua", this.lingua);
        cs.setString("file_name", null);
        cs.setString("dir_spec", null);
        cs.setString("stato_elab", "S");
        cs.setString("descr_elab", null);


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

}

这段代码很好用,除了cs.execute,这给了我这个错误

java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'DBP_MAIN'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

我检查了一千次,参数和类型和数字完全匹配。数据库连接也很好,因为我先做了一些缓存并且它可以工作。

已经尝试删除DBK_PDG_METADATI_CEDOLINO,但没有任何必要。你能帮我弄清楚吗?

标签: javaoraclejdbcplsql

解决方案


  1. 问题可能与可能不支持命名参数的 JDBC 驱动程序有关。

尝试先检查它,例如:

  Connection myConn = . . .   // connection to the RDBMS for Database
  DatabaseMetaData dbmd = myConn.getMetaData();
  if (dbmd.supportsNamedParameters() == true)
  {
      System.out.println("NAMED PARAMETERS FOR CALLABLE"
                        + "STATEMENTS IS SUPPORTED");
  }

如果不是 - 使用参数索引来设置而不是名称......

  1. 该存储过程中是否有任何 OUT 或 INOUT 参数?

如果是这样,您需要使用 注册这些参数registerOutParameterCallableStatement为输出提供一个占位符。


推荐阅读